473,614 Members | 2,084 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

do I need a virtual destrucotr?

Hi all, I have some class hierarchy as follows:

class S {};
class A {};
class B {public: vector<S*> v_; virtual ~B();};

class C : public virtual A, public virtual B { // do I need to define
virtual ~C() so that B can be properly destructed?};

in fact, I don't even know if a destructor in C is even needed if I don't do
any memory allocation? Thanks in advance.

jj.
Jul 19 '05 #1
2 1959
Okay, so for clarification purposes, suppose I have the following:
base (pure virtual)
^
|
|
poly derived1
^
|
|
derived2

and base, derived1, derived2 do not contain anything that needs to be
specially allocated, deallocated. Then I do something like:

base* p = new derived2;

then I do:
delete p;

then only base's destructor will be called... correct? But that's okay,
since stuff inside derived1 and derived2 (such as vector<>, list<>, etc)
should take care of themselves? Or is it necessary to define the destructor
explicitly, and call
vector<>.clear( ) for vectors, lists... etc? It seems extrememly tedious to
go throught the inheritance hierarchy and clearing objects that shouldn't
have to be cleared. Any suggestions? Maybe I'm just confusing myself.

jj

"Rolf Magnus" <ra******@t-online.de> wrote in message
news:bh******** *****@news.t-online.com...
Jimmy Johns wrote:
Hi all, I have some class hierarchy as follows:

class S {};
class A {};
class B {public: vector<S*> v_; virtual ~B();};

class C : public virtual A, public virtual B { // do I need to define
virtual ~C() so that B can be properly destructed?};


That depends. If it's possible that you destroy a C through a pointer to
one of its base classes, you need a virtual destructor, but you will
need that in the base class.
in fact, I don't even know if a destructor in C is even needed if I
don't do any memory allocation?


If you don't need a destructor, don't define one, except if you're in a
base class that is supposed to be used polymorphically , becase then
there is a chance that a derived object is deleted through a pointer to
base, so you'll need a virtual destructor in that base.

Jul 19 '05 #2
Jimmy Johns wrote:
Okay, so for clarification purposes, suppose I have the following:
base (pure virtual)
What is pure virtual? The destructor?
^
|
|
poly derived1
^
|
|
derived2

and base, derived1, derived2 do not contain anything that needs to be
specially allocated, deallocated. Then I do something like:

base* p = new derived2;

then I do:
delete p;

then only base's destructor will be called... correct?
If you mean above that base's destructor is virtual, then no. derived2's
destructor will be called. If OTOH, base's destructor is not virtual,
then you are correct.
But that's okay, since stuff inside derived1 and derived2 (such as
vector<>, list<>, etc) should take care of themselves?
What do you mean by "take care of themselves"? Their destructors must be
called by someone. If derived2's destructor is called, the destructors
of its member variables are called too.
Or is it necessary to define the destructor explicitly, and call
vector<>.clear( ) for vectors, lists... etc?
Their destructors will do that when (and if) they are destroyed
properly.
It seems extrememly tedious to go throught the inheritance hierarchy
and clearing objects that shouldn't have to be cleared. Any
suggestions? Maybe I'm just confusing myself.


I guess you are. It's actually rather simple: If there is a chance (even
if very low) that an instance of derived1 or derived2 is deleted
through a pointer to base, base's destructor must be virtual. Then the
correct destructor is automagically called.

Jul 19 '05 #3

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

Similar topics

5
2208
by: Carl Bevil | last post by:
I'm creating a library for internal use that relies on third-party code. I don't want clients of this library to know anything about the third party code, when compiling or running. Generally I've been achieving this by having an abstract base class which defines an interface, and inheriting a concrete class which defines the implementation. Clients of the library deal only with the base class and request objects of that type from a...
8
2831
by: JustSomeGuy | last post by:
I need to write an new class derived from the list class. This class stores data in the list to the disk if an object that is added to the list is over 1K in size. What methods of the std stl list class must Ioverride in order for this to work?
2
1772
by: Bonj | last post by:
Hello Can anyone assist with the following class hierarcy problem? I have a series of window classes, the object model currently being as such: Window / | \ / | \ MDIClientWindow | TreeViewWindow WndProcWindow / \
10
2258
by: mark | last post by:
I have this class: class Selections { OSStatus Init(); protected: CFMutableSetRef selectionObjects; static void CFASelectionsApplier(const void* value, void* ctx); OSType ready; public: Selections();
8
2888
by: rakoo | last post by:
I want to question about this virtual keyword , what is neccessty of it .. when base class ponter or simply object assingned to derived class object ,we never want that base class funtion by base class object or pointer which pointing to derived class object Are we ? then why stroupstrop has given this keyword
10
2278
by: David Thielen | last post by:
Hi; I have help html pages for each page of my ASP.NET webapp. So for the page datasource.aspx, I have help\datasource.htm. Bu what I want when the hyperlink is clicked, for it to look for the following files in order (assuming I am running from the us): help\datasource_en_US.htm help\datasource_en.htm help\datasource.htm
4
278
by: Neo | last post by:
I have the following code structure- class A { public: virtual void foo_a()=0; }; class B { public: virtual void foo_b()=0; };
3
1580
by: a | last post by:
Hi, I need clarification for virtual method and pure virtual method. e.g Class Base{ virtual void func(){ ---- } } Class Child : public Base{ void func()
0
1800
by: akshaycjoshi | last post by:
I am reading a book which says Even though unboxed value types don't have a type object pointer, you can still call virtual methods (such as Equals, GetHashCode, or ToString) inherited or overridden by the type. The reason is because the CLR can just call these methods nonvirtually and System.ValueType overrides all of these virtual methods and expects the value in the this argument to refer to an unboxed value type instance. Remember, a...
0
8182
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8627
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8279
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8433
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6088
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5540
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4052
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4127
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1425
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.