Copy Constructors and Derived Classes - Copying parent class members
The default constructor for a derived class calls the base class default constructor implicitly, without an explicit call. However, the copy constructor for a derived class does not call the copy constructor of the base class --- it calls the default zero-argument constructor for the base class.
class Base
{
public:
Base() { cout << "Base constructor\n"; } Base(Base &b) { cout << "Base COPY constructor\n"; } }; class Derived : public Base { public: Derived(Derived &b) { cout << "Derived COPY constructor\n"; } }; main() { Derived d1; Derived d2(d1); // copy constructor } This is not what we usually require; more typically a derived class copy constructor will explicitly invoke the base class copy constructor using the special syntax for passing arguments to a base class. class Base { public: Base(Base &b) { cout << "Base COPY constructor\n"; } }; class Derived : public Base { Derived(Derived &b) : Base(b) { cout << "Derived COPY constructor\n"; } }; The Derived object ``b" is passed to the Base class copy constructor. Note that this involves converting the Derived& reference to a Base& reference --- recall that conversion of a pointer/reference is allowed from Derived down to base, but not the reverse.
Assignment Operators and Derived Classes - Copying parent class members
Similarly to the problem with copy constructors in derived classes, the assignment operator in derived classes does not implicitly call the base class assignment operator. In fact, by default, the private data of the base class will not be modified by the derived class assignment operator.
The solution is an explicit call to the Base class assignment operator which can be achieved via the ``this" special pointer, and a type cast to a reference to a reference to the Base class.
void operator=(Derived &d)
{
(Base&)*this = d; // EXPLICIT CALL // The above copies the Base class data
// Now copy the derived class data ....
}
There are a few other alternative methods of explicit calls, such as:
void operator=(Derived &d)
{
(*this).Base::operator=(d); // EXPLICIT CALL
}
(or)
If there is a parent (base) class, those fields must also be copied.
You can accomplish this with the following cryptic statement,
this->Parent::operator=(source); //where Parent is the name of the base class.
//--- file Parent.h
class Parent {...}; // declaration of base class
//--- file Child.h
#include "Parent.h"
class Child : public Parent { // declaration of derived class
public:
Child& Child::operator=(const Child& source);
};//end class Child
//--- file Child.cpp
#include "Child.h"
Child& Child::operator=(const Child& source) {
if (this != &source) {
this->Parent::operator=(source);
. . . // copy all our own fields here.
}
return *this;
}//end operator=
Methods that are implicitly generated by the compiler if they are not explicitly defined are:
a. Default constructor (C::C())
b. Copy constructor (C::C (const C& rhs))
c. Destructor (C::~C())
d. Assignment operator (C& C::operator= (const C& rhs))
e. Address-of operator (C* C::operator&())
f. Address-of operator (const C* C::operator&() const;)
Private Copy Constructor:
1. Use private copy constructor and assignment operator to avoid object copyingIf you don't want users of your class to be able to assign objects of its type (password string objects are a good example), you can declare a private assignment operator and copy constructor.
Please note that the compiler-synthesized copy constructor and assignment operator are public, therefore, you have to define them explicitly as private members in this case.
2. To make sure we can not pass objects by value only by reference.
Links:
http://www.cs.jcu.edu.au/Subjects/cp3120/1996/Lectures/c++/node92.html
C/C++ Memory Corruption And Memory Leaks - http://www.yolinux.com/TUTORIALS/C++MemoryCorruptionAndMemoryLeaks.html