3

The following code will cause C4624. But you can see D is not the sub class of B. Why still got this warning? Thanks!

class B {
// Uncomment the following line to resolve.
// public:
~B();
};

class D  {B b;};   // C4624 B's destructor not public

More details about the warning:

C4624: 'derived class' : destructor could not be generated because a base class destructor is inaccessible

A destructor was not accessible in a base class and was therefore not generated for a derived class. Any attempt to create an object of this type on the stack will cause a compiler error.

fresky
  • 530
  • 5
  • 15

2 Answers2

2

Class members are initialised by the class's constructor(s) and destroyed by its destructor. Their constructor(s) and destructor must therefore be accessible within those functions.

In your example, B has a private destructor, and is therefore not accessible to the destructor of D, which needs it to destroy its member of type B.

I've no idea why the error message refers to a "base class" though. If you get that error message from the code you posted, then your compiler could do with some improvements to its diagnostics.

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
  • I got this warning in Visual studio 2012. I just wondering why the warning said "base class". – fresky Dec 04 '12 at 14:31
  • Most likely, the compiler writer reused the error code without checking whether it was appropriate for its new context. It's a common error, and [can be very expensive](http://archive.eiffel.com/doc/manuals/technology/contract/ariane/). – Mike Seymour Dec 04 '12 at 14:42
0

Because class D isn't friend of class B and class B isn't part of class D. This means that B's private parts can't be accessed from scope of D. This also means that you can't create an object of type D or B on the stack since it will require the destructor of B to be accessible when the object gets out of scope and should be deleted. Note, the error message seems incorrect, like was accurately mentioned by @R.MartinhoFernandes. Private destructors can be of use if you manage the memory by yourself(like described here).

Community
  • 1
  • 1
SomeWittyUsername
  • 18,025
  • 3
  • 42
  • 85