Re: inhibit compiler warning C4624 for a class hierarchy



Ben Voigt <rbv@xxxxxxxxxxxxx> wrote:
"Igor Tandetnik" <itandetnik@xxxxxxxx> wrote in message
news:eERgQSNNHHA.1240@xxxxxxxxxxxxxxxxxxxxxxx
Ben Voigt <rbv@xxxxxxxxxxxxx> wrote:
Every instance will be
freed by the base class destructor and operator delete, regardless
of specific type.

It doesn't work this way. Any class must have a destructor before
you can create (or, to be exact, destroy) an instance of it. It
could be trivial, it could be private, it could be implicitly
defined, but a class must have one. From the standard:

12.4/5 An implicitly-declared destructor is implicitly defined when
it is used to destroy an object of its class type (3.7). A program is
Non-virtual destruction from the base class doesn't use this
implicitly-declared destructor, so this rule doesn't apply.

You mean you are slicing? As in

class B {
~B() {}
public:
void DeleteMe() { delete this; }
};

class D : public B {};

D* pD = new D;
pD->DeleteMe();

In this case, your program exhibits undefined behavior for a different
reason:

5.3.5/3 In the first alternative (delete object), if the static type of
the operand is different from its dynamic type, the static type shall be
a base class of the operand's dynamic type and the static type shall
have a virtual destructor or the behavior is undefined.

I thought your case was more like this:

void B::DeleteMe() {
if (/* this object is actually a D */) {
delete static_cast<D*>(this);
} else {
delete this;
}
}

This program would be ill-formed under 12.4/5

If your architecture is neither, can you show a brief sketch of how your
classes relate to each other?
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925


.



Relevant Pages

  • Re: inhibit compiler warning C4624 for a class hierarchy
    ... Any class must have a destructor before you can ... create (or, to be exact, destroy) an instance of it. ... implicitly-declared destructor, ... a base class with an inaccessible destructor. ...
    (microsoft.public.vc.language)
  • Re: auto_ptr compile error
    ... 12.4/3 If a class has no user-declared destructor, ... An implicitly-declared destructor is an inline ... public member of its class.A destructor is trivial if it is an ...
    (microsoft.public.vc.language)
  • Re: Virtual destructors are unique virtuals right
    ... The destructor of a derived class does not call the ... >> of its base class, regardless of whether the base class has a virtual ... constructor for the BaseClass, then calls the constructor for the ... DerivedClass. ...
    (comp.lang.cpp)
  • Re: Odd behavior, vector member, MFC and consol app
    ... someone call the base class destructor which then ended up doing something bad. ... it should have no virtual functions at all. ... A good clue that a class is not intended for derivation is the absence ...
    (microsoft.public.vc.mfc)
  • Re: Cant compile this code *****SOLVED****
    ... your real program might contain hundreds of code sequences ... that the base class implements a 'virtual' ... destructor, so that when de-allocating objects created via ...
    (alt.comp.lang.learn.c-cpp)