473,394 Members | 1,674 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,394 software developers and data experts.

What is the advantage to declare destructor as a virtual in c++ ?

I have idea about virtual function in c++. It is supported dynamic binding but i have no idea about what is the advantage to declare any destructor as a virtual in c++ ? Explain with suitable example....

Bye...Take Care...Give Reply
Apr 15 '08 #1
6 3898
scruggsy
147 100+
I have idea about virtual function in c++. It is supported dynamic binding but i have no idea about what is the advantage to declare any destructor as a virtual in c++ ? Explain with suitable example....

Bye...Take Care...Give Reply
Here.
Destructors are also sometimes made virtual simply to satisfy the requirement that a polymorphic class must have at least one virtual function.
Apr 15 '08 #2
Banfa
9,065 Expert Mod 8TB
If you delete a class through a base class pointer then if the destructor of the base class is not virtual the destructor of the actual class is not called, the same as for any other function called though a base class pointer.

Removing a class without properly calling its destructor could have serious implications for program stability.
Apr 15 '08 #3
SpecialKay
109 100+
you can avoid memorie leaks.
Apr 15 '08 #4
For work with "dynamic_cast" the class should have a virtual table. so to have a virtual table at least one member function should be virtual. making destructor as virtual satisfy the demand.
Apr 16 '08 #5
I have idea about virtual function in c++. It is supported dynamic binding but i have no idea about what is the advantage to declare any destructor as a virtual in c++ ? Explain with suitable example....

Bye...Take Care...Give Reply

virtual destructer is used so that while deleting the pointer to base class but pointing to base class invokes the derived class destructor first,then the base class destruct hence preventing memory leak...

First pls get clear with virtual function concepts...
suppose we have a class hierachy
Empl-->Manager-->CEO
now we are creating
Empl* e1;
CEO c1;
e1= & c1;
Now when ever my e1 goes out of scope because e1 is of type Empl ,only the destructor of e1 is called & memory is freed for attributes of e1 only. so we have the memory leakage problem
so if we are making Destructor as a virtual so exceutin of destructor will follow
~CEO-->~Manager-->~Empl path so Memory for all the data is freed.....
so to avoid these Memory Leakage problem we have to make destructor as a virtual destructor.....
Apr 16 '08 #6
weaknessforcats
9,208 Expert Mod 8TB
I just published this article in the C/C++ HowTos. Your answer is there.

http://bytes.com/forum/thread793836.html.
Apr 16 '08 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Alex Vinokur | last post by:
http://www.parashift.com/c++-faq-lite/virtual-functions.html#faq-20.6 Faq-20.6 contains function userCode(Shape&) : void userCode(Shape& s) { Shape* s2 = s.clone(); Shape* s3 = s.create();...
4
by: David | last post by:
Hi, Buddy, a newbie's question for you guys, In C++, some functions have a return value type "result", what does this mean, I searched on web, but no hint. thanks a lot David
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...
26
by: pmizzi | last post by:
When i compile my program with the -ansi -Wall -pedantic flags, i get this warning: `class vechile' has virtual functions but non-virtual destructor, and the same with my sub-classes. But when i...
0
by: COHENMARVIN | last post by:
I have a web site where I put my asp.net application in a folder called /asp.net/hotelallocation. I want it to be protected by a password, so I put a web.config file in that folder. But now when...
3
by: Julian | last post by:
I need to convert db field values from "1 - Attempted-Successful" to "Yes", "2 - Attempted-Unsuccessful" to "No" and all other values need to be NULL-ed. So here's my code: Dim strQuery As...
4
by: moleskyca1 | last post by:
What operators cannot be virtual and why? I looked at FAQ and found nothing. I think there are operators that cannot be virtual, but I don't know why?
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...
5
Krithiga Ravi
by: Krithiga Ravi | last post by:
Is it different from cache memory if so, how? And also, what is it main purpose?
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.