Miscellaneous > Programming & Networking

Virtual Destructors?

(1/1)

anphanax:
I got a C++ question.

Why is this legal:
virtual ~classname(void)

When this isn't:
virtual classname(void)

I have yet to figure out a way to override a destructor for a base class, that's why i'm asking.

muzzy:
The latter is a construtor, not a destructor. It doesn't make sense to have a virtual constructor, because virtual methods are a mechanism for call indirection, to invoke the method of actual class instead of the referenced one. Construction is a process where this doesn't happen, so you cannot declare constructors virtual.

Regarding virtual destructors, what's the problem? Just define a new destructor in the descendant class once base class destructor has been declared virtual and you'll be fine.

muzzy:
Since I don't quite get what's your problem, I figured I'd write a short example code to show how it should work. Please be more specific in asking questions if this doesn't help you.


--- Code: ---
#include
#include

using namespace std;

struct Foo {
  Foo() { cout

Navigation

[0] Message Index

Go to full version