Professional C__ - Marc Gregoire [113]
The following code shows a version of Super that lacks a default constructor. The associated version of Sub must explicitly tell the compiler how to call the Super constructor or the code will not compile.
class Super
{
public:
Super(int i);
};
class Sub : public Super
{
public:
Sub();
};
Sub::Sub() : Super(7)
{
// Do Sub's other initialization here.
}
In the preceding code, the Sub constructor passes a fixed value (7) to the Super constructor. Sub could also pass a variable if its constructor required an argument:
Sub::Sub(int i) : Super(i) {}
Passing constructor arguments from the subclass to the superclass is perfectly fine and quite normal. Passing data members, however, will not work. The code will compile, but remember that data members are not initialized until after the superclass is constructed. If you pass a data member as an argument to the parent constructor, it will be uninitialized.
Parent Destructors
Because destructors cannot take arguments, the language can automatically call the destructor for parent classes. The order of destruction is conveniently the reverse of the order of construction:
1. The body of the class’s destructor is called.
2. Any data members of the class are destroyed in the reverse order of their construction.
3. The parent class, if any, is destructed.
Again, these rules apply recursively. The lowest member of the chain is always destructed first. The following code adds destructors to the previous example. The destructors are all declared virtual, which is very important and will be discussed right after this example. If executed, this code will output "123321".
class Something
{
public:
Something() { cout << "2"; }
virtual ~Something() { cout << "2"; }
};
class Parent
{
public:
Parent() { cout << "1"; }
virtual ~Parent() { cout << "1"; }
};
class Child : public Parent
{
public:
Child() { cout << "3"; }
virtual ~Child() { cout << "3"; }
protected:
Something mDataMember;
};
int main()
{
Child myChild;
return 0;
}
Code snippet from ConstructorChain\ConstructorChain.cpp
Notice that the destructors are all virtual. As a rule of thumb, all destructors should be declared virtual. If the preceding destructors were not declared virtual, the code would continue to work fine. However, if code ever called delete on a superclass pointer that was really pointing to a subclass, the destruction chain would begin in the wrong place. For example, the following code is similar to the previous example but the destructors are not virtual. This becomes a problem when a Child object is accessed as a pointer to a Parent and deleted.
class Something
{
public:
Something() { cout << "2"; }
~Something() { cout << "2"; } // Should be virtual, but will work
};
class Parent
{
public:
Parent() { cout << "1"; }
~Parent() { cout << "1"; } // BUG! Make this virtual!
};
class Child : public Parent
{
public:
Child() { cout << "3"; }
~Child() { cout << "3"; } // Should be virtual, but will work
protected:
Something mDataMember;
};
int main()
{
Parent* ptr = new Child();
delete ptr;
return 0;
}
The output of this code is a shockingly terse "1231". When the ptr variable is deleted, only the Parent destructor is called because the destructor was not declared virtual. As a result, the Child destructor is not called and the destructors for its data members are not called.
Technically, you could fix the preceding problem by making the Parent destructor virtual. The “virtualness” would automatically be used by any children. However, we advocate explicitly making all destructors virtual so that you never have to worry about it.
Always make your destructors virtual! The compiler generated default destructor is not virtual, so you should define your own virtual destructor, at least for your parent classes.
Referring to Parent Names
When you override a method in a subclass, you are effectively replacing the original as far as other code is concerned. However, that parent version of the method still exists