Jump to content

  • Log In with Google      Sign In   
  • Create Account

(Pure) virtual base class destructor


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
5 replies to this topic

#1 Juliean   GDNet+   -  Reputation: 2605

Like
0Likes
Like

Posted 04 April 2013 - 08:19 AM

Hello,

 

so I've defined an interface for my renderer like this:

 

#pragma once
#include "ITexture.h"
#include "IFontWriter.h"
#include "ISprite.h"
#include <string>

class IGfx2D
{
public:

	virtual void SetClipRect(const Rect* pClipRect) = 0;
	virtual void SetRenderTarget(UINT id, const ITexture* pTexture) = 0;
	virtual void SetCursor(int offX, int offY, std::wstring sFileName) = 0;

	virtual void ClearTexture(const ITexture& texture) = 0;

	virtual void Begin(void) = 0;
	virtual void End(void) = 0;
};

From this I derive a class with a custom destructor, this destructor never gets called, unless I add an "virtual ~IGfx2D(void) {};" destructor to the interface. Why? This happens to all my (pure) virtual classes without a custom destructor. I assume the compiler (VS 2012) does automatically add a standard destructor if I didn't define one myself. But I though an interface should not have an destructor. Does an interface in c++ really need an virtual destructor or is it some compiler-setting or define I need to do?


Edited by The King2, 04 April 2013 - 08:20 AM.


Sponsor:

#2 Cornstalks   Crossbones+   -  Reputation: 6985

Like
4Likes
Like

Posted 04 April 2013 - 08:47 AM

 Does an interface in c++ really need an virtual destructor or is it some compiler-setting or define I need to do?

Yes, it really does need it. Read this, then read this, and then probably read everything here. The problem, to say it concisely, is that without marking the destructor as "virtual" then when you delete an object, the destructors don't get properly called up/down the inheritance chain. This is only really a problem if you're using polymorphism and delete an object through a pointer to a base object.

 

And for what it's worth, your destructor isn't "pure virtual" (it's just virtual).


Edited by Cornstalks, 04 April 2013 - 08:49 AM.

[ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]

#3 BitMaster   Crossbones+   -  Reputation: 4077

Like
0Likes
Like

Posted 04 April 2013 - 09:08 AM

It's worth noting that at least gcc will warn you about a missing virtual destructor in this scenario.

#4 Juliean   GDNet+   -  Reputation: 2605

Like
0Likes
Like

Posted 04 April 2013 - 09:16 AM

Yes, it really does need it. Read this, then read this, and then probably read everything here. The problem, to say it concisely, is that without marking the destructor as "virtual" then when you delete an object, the destructors don't get properly called up/down the inheritance chain. This is only really a problem if you're using polymorphism and delete an object through a pointer to a base object.

 

Thanks, indeed I knew what a virtual destructor does or how virtual functions in general work (maybe a little more read might not be so bad, though), I was more wondering about what happens if there is no destructor implicitely defined - obviously I need one. Well it seems that for an interface, you might always want a virtual destructor - since anyone might implement the interface in a way requiring to allocate memory.



#5 Cornstalks   Crossbones+   -  Reputation: 6985

Like
1Likes
Like

Posted 04 April 2013 - 09:36 AM

Ah, I see. I think the key to this all is in this statement:

 

But I though an interface should not have an destructor.

C++ has no notion of what an interface is. In C++, there are just classes and structs (which are, effectively, the same thing), and they all work the same. A class with only pure virtual functions isn't "special" from a class that has no virtual functions. "Interfaces" are an idea from other languages, like Java and C#, and the closest thing to an interface in C++ is a class with only pure virtual functions (but again, it's just a normal class; it's not "special").

 

So yes, if you aren't specifying a destructor for that class, one is automatically generated for you (just like it is for any class or struct). Automatically generated destructors are not virtual. Hence, the need to explicitly declare a virtual one.


[ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]

#6 Juliean   GDNet+   -  Reputation: 2605

Like
0Likes
Like

Posted 04 April 2013 - 09:48 AM

C++ has no notion of what an interface is. In C++, there are just classes and structs (which are, effectively, the same thing), and they all work the same. A class with only pure virtual functions isn't "special" from a class that has no virtual functions. "Interfaces" are an idea from other languages, like Java and C#, and the closest thing to an interface in C++ is a class with only pure virtual functions (but again, it's just a normal class; it's not "special").

 

True that, I should mind my wording, I actually meant interface in the sense of those pure virtual classes - I've been recently tought that concept in my c++ class, and been told I wouldn't need a destructor. Thanks still for the explanation.

 

As for the "pure virtual destructor", the headline was meant to read "Pure virtual base class: destructor", my bad...


Edited by The King2, 04 April 2013 - 10:15 AM.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS