When is copy constructor must in a class?
-
I know the reason that when we need to do shallow copy.But how copy constructor resolve this problem?I am not able to understand. So please reply me as soon as possible.
Regards, Pankaj Sachdeva
-
I know the reason that when we need to do shallow copy.But how copy constructor resolve this problem?I am not able to understand. So please reply me as soon as possible.
Regards, Pankaj Sachdeva
Please post questions not related to managed C++ on the Visual C++/MFC board[^] It's up to you to decide how objects are copied, either using the assignment operator or a copy constructor. The difference between the two is right in the names - An assignment operator assigns the "value" of one object to another object. A copy constructor does the same but it's a constructor - it constructs the destination object. The compiler can only provide default copy semantics for objects based on the class of the object. For any class which doesn't provide operators, this means a bitwise, binary copy. In the case of pointers, this may not be what you want. You may want a copy of the data pointed to by a pointer (deep copy), not just a copy of the pointer (shallow copy). To do this you need to implement assignment operator(s) and/or a copy constructor for a class. Mark
Mark Salsbery Microsoft MVP - Visual C++ This episode brought to you by the number 3