Constructor, destructor, overloaded =operator function, copy constructor do not get inherited. What this exactly means?

Viewed 89

Code

#include<iostream>
struct A
{
    ~A()
    {
        std::cout<<"dctorA\n";
    }
};
struct B: public A
{
    ~B()
    {
        std::cout<<"dctorB\n";
    }
};

int main()
{
    B b1;
    b1.~A();
}

Output

dctorA
dctorB
dctorA

if destructor is not inheriting then how I am able to call it through object of B ?

and I know Rule of three and five but compiler work in spit of rule followed or not. so to avoid complexity I avoided writing copy constructor and overloaded= operator.

1 Answers

What that statement is trying to say, is that class B does not automatically get a constructor B::B(int) when you add A::A(int). Similarly, B::operator=(int) is not automatically generated when you have A::operator=(int).

The copy constructor B::B(B const&) can be automatically generated, in which case it expects A::A(A const&). But you see from the argument that it's generated, not inherited as B::B(A const&).

The destructor works the same - generated, not inherited - but that's less visible because there's no parameter.

Related