To tell the truth C + + is used in school before, from graduation to now has been using C embedded programming, now moved out of C + + grammar, if the understanding of the wrong place, but also passing friends more correct ~ ~ ~
constructor is used to construct an object, the main completion of some initialization work, if the class does not provide constructors, the compiler will default to provide a default constructor (the parameter is a null constructor is the default constructor); The destructor is called implicitly, and the Delete object automatically invokes the cleanup of the completed object.
Now it's important to look at the constructors and destructors in the inheritance call:
Copy Code code as follows:
Class A {};
Class B:public A
{};
Class C:public B
{};
c * ptr = new C ();
Delete ptr;
In general, the above code constructor first invokes the constructor of the root parent class, then there is the secondary level of the parent class constructor, which in turn is the constructor of the derived class itself, and the invocation of the parent class constructor is the default constructor of the parent class (or, of course, the Non-default constructor of the parent class can be displayed). That is to say, the derived class first constructs the inherited parent component before constructing itself;
A call to a destructor is called by first invoking the destructor of the derived class itself, then the parent class destructor of the previous layer, until the root parent class destructor, when there is no polymorphism, is called by the destructor.
Change the code above:
A * ptr = new C ();
Delete ptr;
In the case of polymorphism, if the destructor in base class A is not a virtual constructor, when delete ptr is called, only a destructor is invoked, no destructors in B and C are invoked, and all destructors are invoked if the destructor in a is a virtual constructor, and the invocation order is the same as in general.
Change the above code again:
B *prt = new C ();
Delete ptr;
In the case of polymorphism, if the destructor in a, B is not a virtual destructor, then, when delete ptr is called, the destructor of B is invoked, the destructor of a, and the destructor in C is not called, and if at least one of the A or B is a virtual destructor, the destructor call is the same as the general situation.
So summarize the rules:
CA * ptr = new CB ();
Delete ptr;
CB is a subclass of the CA, and the call to the constructor is always the same, when polymorphic:
If neither the CA nor its parent class has a virtual destructor, the destructor of a is called first. The parent class destructor of a is then invoked until the root parent class destructor, which does not invoke the following until the destructor of the derived class; if the CA and its parent class have a virtual destructor, the destructor call is the same as usual.
therefore: a base class with polymorphic properties should declare a virtual destructor function, such that the base class usually has other virtual functions;
If the design of the class is not for the base class and does not have polymorphism, the destructor should not be declared as a virtual destructor
Small Test Code:
Copy Code code as follows:
#include <iostream>
using namespace Std;
Class A
{
Public
A () {cout<< "a constructor" <<ENDL;}
A (char * arp) {cout << "not Default";}
~ca () {cout<< "A desstructor" <<ENDL;}
};
Class B:public A
{
Public
B () {cout<< "B constructor" <<endl;}
~b () {cout<< "B desstructor" <<ENDL;}
};
Class C:public B
{
Public
C (char * arp) {cout<< "C constructor" <<endl;}
~c () {cout<< "C desstructor" <<ENDL;}
};
int main ()
{
c * ptr = new C ("Hello World");
Delete ptr;
}
also mentioned in effective C + +:
1,Destructors cannot spit exceptions, and if a destructor falls out of a function that might produce an exception, the capture is handled inside the destructor, because if the destructor throws an exception, such as vector, it can cause multiple exceptions to be thrown when the destructor of each object is called for deletion. This allows the program to enter an indeterminate state.
2 . If the user needs to respond to an exception that is thrown during the operation of an action function, class should provide a normal function to perform this operation.
3 . Virtual functions should not be invoked in constructors and destructors. This is because when the constructor is called, the constructor of the parent class is invoked first, at which point the object's type appears to be a parent class object (in fact, the subclass member is still in an indeterminate state). The virtual function of the parent class is invoked without invoking the virtual function of the subclass.