EDIT: Summary of Answers
Subsequently, B is a subclass of A.
This is a matter of terminology; ctors and dtors are not inherited in the sense that ctor / dtor of B will not be borrowed from interface A. The class has at least one constructor and has exactly one destructor.
- Constructors :
- B does not inherit constructors from A;
- If B ctor explicitly calls one of A ctor, by default ctor from A will be called automatically before the body of B ctor (the idea is that A needs to be initialized before B is created).
- destructors :
- B does not inherit A dtor;
- After it exits, destructor B will automatically call the destructor.
Acknowledgment: I would especially like to thank Oli Charlworth and Kos for their answers, and I put the Kos solution as the solution because it was the one I understood best.
ORIGINAL MAIL
When you search "C ++ destructor inheritance site: stackoverflow.com" on Google, you currently find the following posts:
- Inheritance of constructor and destructor : two users with a 30k + reputation say that they are inherited and that it is not
- Are virtual destructors inherited? : nothing is mentioned here, which indicates that destructors are not inherited
- Destructors and inheritance in C ++? : comments show that destructors are inherited
Q1: What I also know from practice is that you cannot initialize a derived object using the same prototype as its parent constructor, without explicitly defining the constructor for the derived class, is this correct?
Despite the fact that from the messages that seem to be inherited, itβs clear enough that I'm still puzzled by the fact that a user with a 32x reputation will say that this is not the case. I wrote a small example that should clarify all thoughts:
#include <cstdio> /******************************/ // Base class struct A { A() { printf( "\tInstance counter = %d (ctor)\n", ++instance_counter ); } ~A() { printf( "\tInstance counter = %d (dtor)\n", --instance_counter ); } static int instance_counter; }; // Inherited class with default ctor/dtor class B : public A {}; // Inherited class with defined ctor/dtor struct C : public A { C() { printf("\tC says hi!\n"); } ~C() { printf("\tC says bye!\n"); } }; /******************************/ // Initialize counter int A::instance_counter = 0; /******************************/ // A few tests int main() { printf("Create A\n"); A a; printf("Delete A\n"); a.~A(); printf("Create B\n"); B b; printf("Delete B\n"); b.~B(); printf("Create new B stored as A*\n"); A *a_ptr = new B(); printf("Delete previous pointer\n"); delete a_ptr; printf("Create C\n"); C c; printf("Delete C\n"); c.~C(); }
and here is the result (compiled with g ++ 4.4.3):
Create A Instance counter = 1 (ctor) Delete A Instance counter = 0 (dtor) Create B Instance counter = 1 (ctor) Delete B Instance counter = 0 (dtor) Create new B stored as A* Instance counter = 1 (ctor) Delete previous pointer Instance counter = 0 (dtor) Create C Instance counter = 1 (ctor) C says hi! Delete C C says bye! Instance counter = 0 (dtor) // We exit main() now C says bye! Instance counter = -1 (dtor) Instance counter = -2 (dtor) Instance counter = -3 (dtor)
Q2: Could someone who believes that this has not been inherited explain that?
Q3: So, what happens when you call the constructor of a subclass with inputs? Is the "empty constructor" of the superclass called?
c ++ inheritance constructor destructor
Sheljohn Jan 6 '13 at 16:46 2013-01-06 16:46
source share