[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

virtual fn, destructor

DavidW

12/11/2008 11:42:00 PM

Hello,

I just to confirm a rule.

class B
{
public:
virtual ~B() {}
virtual void f();
};

class D : public B
{
public:
void f();
};

The virtual ~B() is necessary because B has at least one virtual function, but
an explicit d'tor is not required in derived classes. Correct?


52 Answers

joseph cook

12/12/2008 12:05:00 AM

0

On Dec 11, 6:42 pm, "DavidW" <n...@email.provided> wrote:
> Hello,
>
> I just to confirm a rule.
>
> class B
> {
> public:
>     virtual ~B() {}
>     virtual void f();
>
> };
>
> class D : public B
> {
> public:
>     void f();
>
> };
>
> The virtual ~B() is necessary because B has at least one virtual function, but
> an explicit d'tor is not required in derived classes. Correct?

Yes. You are correct. You will have a lot of people respond with a
lot of detail about when you really need, and when you really don't
need a virtual destructor, but "virtual destructor required when there
are any virtual functions" is a good simple rule to live by.

Joe C

Triple-DES

12/12/2008 12:13:00 AM

0

On 12 Des, 00:42, "DavidW" <n...@email.provided> wrote:
> Hello,
>
> I just to confirm a rule.
>
> class B
> {
> public:
>     virtual ~B() {}
>     virtual void f();
>
> };
>
> class D : public B
> {
> public:
>     void f();
>
> };
>
> The virtual ~B() is necessary because B has at least one virtual function,

If you are going to delete D objects through a pointer to B, then the
virtual destructor is necessary, even if there are no other virtual
functions.

> but an explicit d'tor is not required in derived classes. Correct?

Correct.

DavidW

12/12/2008 12:31:00 AM

0

Triple-DES wrote:
> On 12 Des, 00:42, "DavidW" <n...@email.provided> wrote:
>> Hello,
>>
>> I just to confirm a rule.
>>
>> class B
>> {
>> public:
>> virtual ~B() {}
>> virtual void f();
>>
>> };
>>
>> class D : public B
>> {
>> public:
>> void f();
>>
>> };
>>
>> The virtual ~B() is necessary because B has at least one virtual
>> function,
>
> If you are going to delete D objects through a pointer to B, then the
> virtual destructor is necessary, even if there are no other virtual
> functions.

Right, but my question is more about the standard. I thought that it actually
demanded the virtual destructor in B, whether you delete a D via a B* or not.


DavidW

12/12/2008 12:31:00 AM

0

joecook@gmail.com wrote:
> On Dec 11, 6:42 pm, "DavidW" <n...@email.provided> wrote:
>> Hello,
>>
>> I just to confirm a rule.
>>
>> class B
>> {
>> public:
>> virtual ~B() {}
>> virtual void f();
>>
>> };
>>
>> class D : public B
>> {
>> public:
>> void f();
>>
>> };
>>
>> The virtual ~B() is necessary because B has at least one virtual
>> function, but an explicit d'tor is not required in derived classes.
>> Correct?
>
> Yes. You are correct. You will have a lot of people respond with a
> lot of detail about when you really need, and when you really don't
> need a virtual destructor, but "virtual destructor required when there
> are any virtual functions" is a good simple rule to live by.

Okay, thanks.


joseph cook

12/12/2008 1:15:00 AM

0

On Dec 11, 7:30 pm, "DavidW" <n...@email.provided> wrote:
> Triple-DES wrote:
> > On 12 Des, 00:42, "DavidW" <n...@email.provided> wrote:

> Right, but my question is more about the standard. I thought that it actually
> demanded the virtual destructor in B, whether you delete a D via a B* or not.

No, it does not require it.
Joe Cook

Triple-DES

12/12/2008 2:11:00 AM

0

On 12 Des, 01:30, "DavidW" <n...@email.provided> wrote:
> Triple-DES wrote:
> > If you are going to delete D objects through a pointer to B, then the
> > virtual destructor is necessary, even if there are no other virtual
> > functions.
>
> Right, but my question is more about the standard. I thought that it actually
> demanded the virtual destructor in B, whether you delete a D via a B* or not.

It does not, as Joe pointed out. That would make standard classes like
std::less ill-formed, since they inherit from std::binary_function
(which has a public non-virtual dtor).

The virtual dtor requirement is given in 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.

DavidW

12/12/2008 2:37:00 AM

0

Triple-DES wrote:
> On 12 Des, 01:30, "DavidW" <n...@email.provided> wrote:
>> Triple-DES wrote:
>>> If you are going to delete D objects through a pointer to B, then
>>> the virtual destructor is necessary, even if there are no other
>>> virtual functions.
>>
>> Right, but my question is more about the standard. I thought that it
>> actually demanded the virtual destructor in B, whether you delete a
>> D via a B* or not.
>
> It does not, as Joe pointed out. That would make standard classes like
> std::less ill-formed, since they inherit from std::binary_function
> (which has a public non-virtual dtor).

The std::less and std::binary_function in one compiler I looked at don't contain
any virtual functions, though.

> The virtual dtor requirement is given in 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.

That must be what I was confusing it with. I knew there was a case where no
virtual d'tor was non-conforming. Thanks.


Triple-DES

12/12/2008 3:24:00 AM

0

On 12 Des, 03:37, "DavidW" <n...@email.provided> wrote:
> Triple-DES wrote:
> > On 12 Des, 01:30, "DavidW" <n...@email.provided> wrote:
> >> Triple-DES wrote:
> >>> If you are going to delete D objects through a pointer to B, then
> >>> the virtual destructor is necessary, even if there are no other
> >>> virtual functions.
>
> >> Right, but my question is more about the standard. I thought that it
> >> actually demanded the virtual destructor in B, whether you delete a
> >> D via a B* or not.
>
> > It does not, as Joe pointed out. That would make standard classes like
> > std::less ill-formed, since they inherit from std::binary_function
> > (which has a public non-virtual dtor).
>
> The std::less and std::binary_function in one compiler I looked at don't contain
> any virtual functions, though.

Right, you were talking about whether it was required for base classes
_with virtual functions_. I just wanted to give an example of a base
class with a non-virtual dtor.

SG

12/12/2008 8:40:00 AM

0

On 12 Dez., 00:42, "DavidW" <n...@email.provided> wrote:
> I just to confirm a rule.
>
> class B
> {
> public:
>     virtual ~B() {}
>     virtual void f();
>
> };
>
> class D : public B
> {
> public:
>     void f();
>
> };
>
> The virtual ~B() is necessary because B has at least one virtual function

It's only necessary if you intend to delete an object of a derived
type through a pointer to the base class. If you DON'T need this you
can make the dtor PROTECTED and non-virtual in the base class.

Cheers!
SG

James Kanze

12/12/2008 9:41:00 AM

0

On Dec 12, 1:05 am, joec...@gmail.com wrote:
> On Dec 11, 6:42 pm, "DavidW" <n...@email.provided> wrote:
> > I just to confirm a rule.

> > class B
> > {
> > public:
> > virtual ~B() {}
> > virtual void f();
> > };

> > class D : public B
> > {
> > public:
> > void f();
> > };

> > The virtual ~B() is necessary because B has at least one
> > virtual function, but an explicit d'tor is not required in
> > derived classes. Correct?

> Yes. You are correct. You will have a lot of people respond
> with a lot of detail about when you really need, and when you
> really don't need a virtual destructor, but "virtual
> destructor required when there are any virtual functions" is a
> good simple rule to live by.

Actually, the usual rule is that the base class destructor must
be either virtual or protected. And of course, it is a
"programming standards" rule, not something imposed by the
standard.

The standard actually violates it in several cases. Arguably,
it shouldn't, and the classes in question should have empty
protected destructors. Except that that would impose
restrictions on classes deriving from them, since no class
deriving from them would have a trivial destructor.

--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34