Why can't we name protected destructors from a derived class?

I know about the use of private (and, of course, public) destructors.

I also know about using a protected destructor in a derived class:

Use a protected destructor to prevent the destruction of a derived object using a base class pointer

But I tried to run the following code and it will not compile:

struct A{ int i; A() { i = 0;} protected: ~A(){} }; struct B: public A{ A* a; B(){ a = new A();} void f(){ delete a; } }; int main() { B b= B(); bf(); return 0; } 

I get:

 void B::f()': main.cpp:9:16: error: 'A::~A()' is protected 

What am I missing?

If I called a protected method from inside f (), it will work. So why is the name d'or?

+7
c ++ inheritance oop c ++ 11 destructor
source share
1 answer

protected does not mean that your B can access members of any A ; it only means that he can access those members of his own base A ... and the elements of some other base B A !

This contrasts with private , as a result of which an object of type A can always call private elements of another object of type A

+11
source share

All Articles