Sign in to follow this  
Juliean

(Pure) virtual base class destructor

Recommended Posts

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

Share this post


Link to post
Share on other sites

 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

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this