(Pure) virtual base class destructor

Started by
4 comments, last by Juliean 11 years ago

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?

Advertisement

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).

[size=2][ 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 ]
It's worth noting that at least gcc will warn you about a missing virtual destructor in this scenario.

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.

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.

[size=2][ 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 ]

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...

This topic is closed to new replies.

Advertisement