C++ Constructor/Destructor inheritance

后端 未结 7 2152
醉酒成梦
醉酒成梦 2020-11-30 19:10

EDIT : Summary of answers

In the following, B is a subclass of A.

It\'s a matter of terminology; ctors and dtors are not in

7条回答
  •  一生所求
    2020-11-30 19:36

    Inheritance is what : mechanism of reusing and extending existing classes without modifying them, thus producing hierarchical relationships between them.

    Inheritance is almost like embedding an object into a class.

    when class is inheriting a base class then the base class's constructor is called first then derived class's ,and the destructor's call is in reverse order.

    So Why Base Class Constructor is called (called not inherited may be with parameters/default) : to guarantees that the base class is properly constructed when the constructor for the derived class is executed.

    Now Calling of Destructor (calling not inherit) : when base object get out of scope then the destructor is called on its own.so there is np issue of inheritance of destructor.

    now your questions:

    ans 1 - yes you are correct for first question.
    ans 2 - so destructor is called not inherited after the scope of object goes out.
    & ans 3 - if in derived class you are giving the call with parameters then only that constructor would get called , with it no other constructor would get called.
    there is no point of issuse that 2 constructor of same object would get called on object creation,as constructor called at the creation of an object. It prepares the new object for use.so there is no logic of preparing the object twice with different constructors.

提交回复
热议问题