473,320 Members | 1,969 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Why for the Virtual Destrcutors

Hi to all. Please i can't get information why the Virtual Destructors
and for what purpose they are.

Aug 3 '05 #1
7 1137

sabarish wrote:
Hi to all. Please i can't get information why the Virtual Destructors
and for what purpose they are.


To prperly destroy objects when they are deleted via a pointer to the
base class.
Example:

class Base
{
public:
virtual ~Base() {}
};

class Derived : public Base
{
public:
virtual ~Derived() {}
};

int main()
{
Base* b = new Derived;

delete b; // <- this will invoke the Derived destructor

return 0;
}

//dan

Aug 3 '05 #2
sabarish wrote:
Hi to all. Please i can't get information why the Virtual Destructors
and for what purpose they are.


Virtual Destructor is mainly used whenever the object needs to be
completeltly deleted. That is without virtual destructor, the base
class destructor alone will be invoked which deletes base class part
alone which is not our intention. The derived class objects allocation
has also to be deleted which is only possible using virtual destructor.

-Wg-

Aug 3 '05 #3
> Hi to all. Please i can't get information why the Virtual Destructors
and for what purpose they are.


To properly destroy an object destructors must be called from the outside
in, i.e. from the most derived object towards the base(s) recursively.
Virtual destructor ensures that a call to the destructor from one of the
bases is dispatched to the most derived class's destructor.

ben
Aug 3 '05 #4
WittyGuy wrote:
That is without virtual destructor, the base
class destructor alone will be invoked which deletes base class part
alone which is not our intention.


That is often what happens, but technically the behavior is undefined.
So don't rely on this happening.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Aug 3 '05 #5

"benben" <be******@hotmail.com> wrote in message
news:42***********************@news.optusnet.com.a u...
Hi to all. Please i can't get information why the Virtual Destructors
and for what purpose they are.


To properly destroy an object destructors must be called from the outside
in, i.e. from the most derived object towards the base(s) recursively.
Virtual destructor ensures that a call to the destructor from one of the
bases is dispatched to the most derived class's destructor...


.... when deleting an object via a pointer-to-base-class. (If you're using a
pointer-to-derived-class, or if an automatic object of derived-class type is
destroyed, then the virtual destructor isn't required.)

-Howard
Aug 3 '05 #6
Pete Becker wrote:
That is often what happens, but technically the behavior is undefined.
So don't rely on this happening.


Technically speaking, could you tell me where the member variables will
be residing for different specifiers? How then, data hiding is
implemented?

thanks
-Wg-

Aug 4 '05 #7
Surely the behavior is defined, the derived part of the object is not
destroyed,
so you have memory resource leaks, or is there something else? Ok, I guess
if the derived class managed some thread, the thread may still be running
after
the object was "destroyed" and now could try to access something in the base
class
which is now gone..

"Pete Becker" <pe********@acm.org> wrote in message
news:HI********************@rcn.net...
WittyGuy wrote:
That is without virtual destructor, the base
class destructor alone will be invoked which deletes base class part
alone which is not our intention.


That is often what happens, but technically the behavior is undefined.
So don't rely on this happening.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)

Oct 2 '05 #8

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

Similar topics

4
by: JKop | last post by:
I'm starting to think that whenever you derive one class from another, that you should use virtual inheritance *all* the time, unless you have an explicit reason not to. I'm even thinking that...
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...
32
by: Adrian Herscu | last post by:
Hi all, In which circumstances it is appropriate to declare methods as non-virtual? Thanx, Adrian.
11
by: ypjofficial | last post by:
Hello All, So far I have been reading that in case of a polymorphic class ( having at least one virtual function in it), the virtual function call get resolved at run time and during that the...
2
by: Heinz Ketchup | last post by:
Hello, I'm looking to bounce ideas off of anyone, since mainly the idea of using Multiple Virtual Inheritance seems rather nutty. I chalk it up to my lack of C++ Experience. Here is my...
5
by: druberego | last post by:
I read google and tried to find the solution myself. YES I do know that you can get undefined references if you: a) forget to implement the code for a prototype/header file item, or b) you forget...
7
by: eric | last post by:
hello i'm confused by an example in the book "Effective C++ Third Edition" and would be grateful for some help. here's the code: class Person { public: Person(); virtual ~Person(); // see...
17
by: Jess | last post by:
Hello, If I have a class that has virtual but non-pure declarations, like class A{ virtual void f(); }; Then is A still an abstract class? Do I have to have "virtual void f() = 0;"...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.