Constructors, Destructors, and Copy Assignment Operators
Three special kinds of member functions are never inherited:
- Copy constructors
- Copy assignment operators
- Destructors
These three functions are generated automatically by the compiler for classes that do not specify them.
Why are these functions special?
The base class functions are not sufficient to initialize, copy, or destroy a derived instance.
Constructors
For a class that inherits from another, the base class constructor must be called as part of its initialization process. The derived constructor may specify which base class constructor is called in its initialization list.
A class with no constructors is automatically given a compiler-generated default constructor that calls the default constructor for each of its base classes. If a class has some constructors but no default constructor, then it has no default initialization. In this case, any derived class constructor must make a specific base class constructor call in its initialization list.
Order of Initialization
Initialization proceeds in the following order:
- Base classes first, in the order in which they are listed in the classHead of the derived class
- Data members, in declaration order
Copy Assignment Operators
A copy assignment operator will be generated automatically by the compiler for each class that does not have one explicitly defined for it. It calls its base class operator= and then performs memberwise assignments in declaration order.
Other member function operators are inherited the same way as normal member functions.
Copy Constructors
Like the copy assignment operator, the copy constructor gets generated automatically for classes that do not have one defined. The compiler-generated copy constructor will carry out member-by-member initialization, much as one would expect.
Example 6.20. src/derivation/assigcopy/account.h
[ . . . . ] class Account { public: Account(unsigned acctNum, double balance, string owner); virtual ~Account(); private: unsigned m_AcctNum; double m_Balance; string m_Owner; }; |
In Example 6.20, we defined a single constructor that takes arguments, so this class has no default constructor (i.e., the compiler will not generate one for us).
We did not define a copy constructor, which means the compiler will generate one for us. Therefore, this class can be initialized in exactly two ways.
Example 6.21. src/derivation/assigcopy/account.h
[ . . . . ] class JointAccount : public Account { public: JointAccount (unsigned acctNum, double balance, string owner, string jowner); JointAccount(const Account & acct, string jowner); ~JointAccount(); private: string m_JointOwner; }; |
In the derived class defined in Example 6.21, we have two constructors. Both of them require base class initialization.
Example 6.22. src/derivation/assigcopy/account.cpp
[ . . . . ] #include "account.h" #include Account::Account(unsigned acctNum, double balance, string owner) { m_Balance=balance; m_AcctNum = acctNum; m_Owner = owner; } JointAccount::JointAccount (unsigned acctNum, double balance, string owner, string jowner) :Account(acctNum, balance, owner), m_JointOwner(jowner) { <-- 1 } JointAccount::JointAccount (const Account& acc, string jowner) :Account(acc) { <-- 2 m_JointOwner = jowner; } (1)Base class initialization is required. (2)Compiler-generated copy constructor is called. |
In Example 6.22, the compiler allows JointAccount::JointAccount to call Account(const Account&), even though there isn't one defined. The compiler-generated copy constructor will do a memberwise copy.
Destructors
Destructors are not inherited. Just as with the copy constructor and copy assignment operator, the compiler will generate a destructor if we do not define one explicitly. Base class destructors are automatically called on all derived objects regardless of whether a destructor is defined in the class. Members and base-class parts are destroyed in the reverse order of initialization.