Connecting Tech Pros Worldwide Forums | Help | Site Map

If declared as virtual in base, its derived version also is virtual. Why not for destructors?

qazmlp
Guest
 
Posts: n/a
#1: Jul 22 '05
When a member function is declared as virtual in the base class, the
derived class versions of it are always treated as virtual.

I am just wondering, why the same concept was not used for the
destructors.
What I am expecting is, if the destructor is declared as virtual in
base, the destructors of all its derived classes also should be
virtual always.
What exactly is the reason for not having it so?

Thanks!

Rob Williscroft
Guest
 
Posts: n/a
#2: Jul 22 '05

re: If declared as virtual in base, its derived version also is virtual. Why not for destructors?


qazmlp wrote in news:db9bbf31.0407250622.43d92c3e@posting.google.c om in
comp.lang.c++:
[color=blue]
> When a member function is declared as virtual in the base class, the
> derived class versions of it are always treated as virtual.
>
> I am just wondering, why the same concept was not used for the
> destructors.
> What I am expecting is, if the destructor is declared as virtual in
> base, the destructors of all its derived classes also should be
> virtual always.
> What exactly is the reason for not having it so?[/color]

It is So, you have been missinformed.

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Phlip
Guest
 
Posts: n/a
#3: Jul 22 '05

re: If declared as virtual in base, its derived version also is virtual. Why not for destructors?


qazmlp wrote:
[color=blue]
> When a member function is declared as virtual in the base class, the
> derived class versions of it are always treated as virtual.
>
> I am just wondering, why the same concept was not used for the
> destructors.
> What I am expecting is, if the destructor is declared as virtual in
> base, the destructors of all its derived classes also should be
> virtual always.
> What exactly is the reason for not having it so?[/color]

Uh, constructors can't be virtual. Destructors inherit virtuality. What
evidence are you working with?

--
Phlip
http://industrialxp.org/community/bi...UserInterfaces


JKop
Guest
 
Posts: n/a
#4: Jul 22 '05

re: If declared as virtual in base, its derived version also is virtual. Why not for destructors?


qazmlp posted:
[color=blue]
> When a member function is declared as virtual in the base class, the
> derived class versions of it are always treated as virtual.
>
> I am just wondering, why the same concept was not used for the
> destructors.
> What I am expecting is, if the destructor is declared as virtual in
> base, the destructors of all its derived classes also should be
> virtual always.
> What exactly is the reason for not having it so?
>
> Thanks![/color]


Bullshit.


class Mammal
{
public:

virtual ~Mammal() {}
}

class Primate : public Mammal {};

class Human : public Primate {};

int main()
{
Mammal* john = new Human;

delete john;
}



-JKop
Phlip
Guest
 
Posts: n/a
#5: Jul 22 '05

re: If declared as virtual in base, its derived version also is virtual. Why not for destructors?


JKop wrote:
[color=blue]
> Bullshit.[/color]

One can usually masquerade such derision within false diplomacy...
[color=blue]
>
> class Mammal
> {
> public:
>
> virtual ~Mammal() {}
> }
>
> class Primate : public Mammal {};
>
> class Human : public Primate {};
>
> int main()
> {
> Mammal* john = new Human;
>
> delete john;
> }[/color]

That applies the test "it compiles, runs, and does not crash".

To apply the test "my compiler interprets the standard like this", put a
cout statement inside the derived destructor.

--
Phlip
http://industrialxp.org/community/bi...UserInterfaces


JKop
Guest
 
Posts: n/a
#6: Jul 22 '05

re: If declared as virtual in base, its derived version also is virtual. Why not for destructors?


Phlip posted:
[color=blue]
> To apply the test "my compiler interprets the standard[/color]
like this", put a[color=blue]
> cout statement inside the derived destructor.[/color]


I prefered my way. If the person is bothered, they'll do
what needs to be done. If they're not bothered, my
assistance will have been of no benefit.


-JKop

David Rubin
Guest
 
Posts: n/a
#7: Jul 22 '05

re: If declared as virtual in base, its derived version also is virtual. Why not for destructors?


JKop <NULL@NULL.NULL> wrote in message news:<RnTMc.5496$Z14.6828@news.indigo.ie>...

[snip][color=blue][color=green]
> > What I am expecting is, if the destructor is declared as virtual in
> > base, the destructors of all its derived classes also should be
> > virtual always.
> > What exactly is the reason for not having it so?[/color][/color]
[color=blue]
> Bullshit.[/color]
[color=blue]
> class Mammal
> {
> public:[/color]
[color=blue]
> virtual ~Mammal() {}
> }[/color]
[color=blue]
> class Primate : public Mammal {};[/color]
[color=blue]
> class Human : public Primate {};[/color]
[color=blue]
> int main()
> {
> Mammal* john = new Human;[/color]
[color=blue]
> delete john;
> }[/color]

How does this illustrate that the "virtual-ness" of destructors is inherited?

/david
Howard
Guest
 
Posts: n/a
#8: Jul 22 '05

re: If declared as virtual in base, its derived version also is virtual. Why not for destructors?



"qazmlp" <qazmlp1209@rediffmail.com> wrote in message
news:db9bbf31.0407250622.43d92c3e@posting.google.c om...[color=blue]
> When a member function is declared as virtual in the base class, the
> derived class versions of it are always treated as virtual.
>
> I am just wondering, why the same concept was not used for the
> destructors.
> What I am expecting is, if the destructor is declared as virtual in
> base, the destructors of all its derived classes also should be
> virtual always.
> What exactly is the reason for not having it so?
>
> Thanks![/color]

But it *is* so!

Try this test: write a class A, with a virtual destructor. Write a class B
publicly deriving from A, but declare its destructor *without* the virtual
keyword. Then write a third class C publicly deriving from B, again without
the virtual keyword in the destructor declaration. Put some cout code in
each destructor to see which ones get called. In main (or some other
function), declare a B* pointer, and instantiate it via "new C()". Then
call delete on the pointer. You should see that the destructor for C is
called. That's because the destructor for B is virtual, even though you
didn't give it the virtual keyword. The only place you had the virtual
keyword was in the A class, yet you're able to delete a C object via a
pointer to a B object, which is only possible if the base class (B) has a
virtual destructor. So B must have inherited the "virtualness" of its
destructor from A. Q.E.D.

(But, I would advise you to always add the virtual keyword to any function
that overrides a base class' virtual function, including your destructors,
just so you don't have to go hunting up the inheritance tree later to see if
a function is inheriting virtual behavior or not.)

-Howard





Closed Thread


Similar C / C++ bytes