472,356 Members | 487 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,356 software developers and data experts.

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

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!
Jul 22 '05 #1
7 1803
qazmlp wrote in news:db**************************@posting.google.c om in
comp.lang.c++:
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?


It is So, you have been missinformed.

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #2
qazmlp wrote:
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?


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

--
Phlip
http://industrialxp.org/community/bi...UserInterfaces
Jul 22 '05 #3
qazmlp posted:
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!

Bullshit.
class Mammal
{
public:

virtual ~Mammal() {}
}

class Primate : public Mammal {};

class Human : public Primate {};

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

delete john;
}

-JKop
Jul 22 '05 #4
JKop wrote:
Bullshit.
One can usually masquerade such derision within false diplomacy...

class Mammal
{
public:

virtual ~Mammal() {}
}

class Primate : public Mammal {};

class Human : public Primate {};

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

delete john;
}


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
Jul 22 '05 #5
Phlip posted:
To apply the test "my compiler interprets the standard like this", put a cout statement inside the derived destructor.

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

Jul 22 '05 #6
JKop <NU**@NULL.NULL> wrote in message news:<Rn*****************@news.indigo.ie>...

[snip]
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?
Bullshit. class Mammal
{
public: virtual ~Mammal() {}
} class Primate : public Mammal {}; class Human : public Primate {}; int main()
{
Mammal* john = new Human; delete john;
}


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

/david
Jul 22 '05 #7

"qazmlp" <qa********@rediffmail.com> wrote in message
news:db**************************@posting.google.c om...
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!


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

Jul 22 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

23
by: ctick | last post by:
A reason for declaring a "virtual destructor" for a Base class is to make sure the destructor of Derived class will be invoked when a pointer of Base type is used to delete an object of Derived. ...
24
by: Shao Zhang | last post by:
Hi, I am not sure if the virtual keyword for the derived classes are required given that the base class already declares it virtual. class A { public: virtual ~A();
23
by: heted7 | last post by:
Hi, Most of the books on C++ say something like this: "A virtual destructor should be defined if the class contains at least one virtual member function." My question is: why is it only for...
11
by: santosh | last post by:
Hello, I was going through the Marshal Cline's C++ FAQ-Lite. I have a doubt regarding section 33.10. Here he is declaring a pure virtual destructor in the base class. And again defining...
10
by: amparikh | last post by:
Ok, my question is not about Virtual destructors and why, but more on the significance. Generally we have a virtual destructor in the base class ( and inadvertently in the derived class) so that...
8
by: LAvoisieR | last post by:
Following test code behaviour suprised me and I dont know what's wrong with this. I have two overloaded constructors within base class and virtual destructor implemented. Derived class uses...
4
by: Zeng | last post by:
I often run into situation where I would like to have a method such as that has derived behavior similar to destructors in c++, is that possible? public BaseClass { private int m_dataInBase;...
7
by: Markus Svilans | last post by:
Hello, My question involves virtual functions and inheritance. Suppose we have a class structure, that consists of "data" classes, and "processor" classes. The data classes are derived from...
9
by: desktop | last post by:
On this page: http://www.eptacom.net/pubblicazioni/pub_eng/mdisp.html Shape specify the virtual function: virtual double Intersect( const Shape& s) = 0; then the derived class Circle...
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
1
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. header("Location:".$urlback); Is this the right layout the...
1
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.