Re: inhibit compiler warning C4624 for a class hierarchy
- From: "Igor Tandetnik" <itandetnik@xxxxxxxx>
- Date: Wed, 10 Jan 2007 14:58:21 -0500
Ben Voigt <rbv@xxxxxxxxxxxxx> wrote:
"Igor Tandetnik" <itandetnik@xxxxxxxx> wrote in message
news:eERgQSNNHHA.1240@xxxxxxxxxxxxxxxxxxxxxxx
Ben Voigt <rbv@xxxxxxxxxxxxx> wrote:Non-virtual destruction from the base class doesn't use this
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
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
.
- Follow-Ups:
- Re: inhibit compiler warning C4624 for a class hierarchy
- From: Ben Voigt
- Re: inhibit compiler warning C4624 for a class hierarchy
- References:
- inhibit compiler warning C4624 for a class hierarchy
- From: Ben Voigt
- Re: inhibit compiler warning C4624 for a class hierarchy
- From: Alex Blekhman
- Re: inhibit compiler warning C4624 for a class hierarchy
- From: Heinz Ozwirk
- Re: inhibit compiler warning C4624 for a class hierarchy
- From: Alex Blekhman
- Re: inhibit compiler warning C4624 for a class hierarchy
- From: Ben Voigt
- Re: inhibit compiler warning C4624 for a class hierarchy
- From: Igor Tandetnik
- Re: inhibit compiler warning C4624 for a class hierarchy
- From: Ben Voigt
- inhibit compiler warning C4624 for a class hierarchy
- Prev by Date: Check internet connection
- Next by Date: Re: Check internet connection
- Previous by thread: Re: inhibit compiler warning C4624 for a class hierarchy
- Next by thread: Re: inhibit compiler warning C4624 for a class hierarchy
- Index(es):
Relevant Pages
|