[ fromfile: derivedcdas.xml id: derivedcdas ]
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. |
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.
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
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.
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 class with a single constructor that takes arguments, so Account 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 Example 6.21, we have a derived class with two constructors. Both of them require base class initialization.
Example 6.22. src/derivation/assigcopy/account.cpp
[ . . . . ]
#include "account.h"
#include <iostream>
Account::Account(unsigned acctNum, double balance, string owner) :
m_AcctNum(acctNum), m_Balance(balance), m_Owner(owner)
{ }
JointAccount::JointAccount (unsigned acctNum, double balance,
string owner, string jowner)
:Account(acctNum, balance, owner),
m_JointOwner(jowner)
{ }
JointAccount::JointAccount (const Account& acc, string jowner)
:Account(acc),
m_JointOwner(jowner)
{ }
In Example 6.22, the compiler allows JointAccount::JointAccount to use Account(const Account&) for initialization, even though we have not defined it.
The compiler-generated copy constructor will do a memberwise copy.
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.
| Generated: $Date: 2009-09-08 12:15:32 -0400 (Tue, 08 Sep 2009) $ | © 2009 Alan Ezust and Paul Ezust. |