views:

151

answers:

2

When creating prototype classes I lay out the destructor as such:

virtual ~MyClass();

When finalizing the class in a library I noticed that I cannot add 'virtual'. Is this normal, and is virtual taken into consideration or am I do something wrong?

For example; when I try to do this I get a compiler error:

virtual MyClass::~MyClass() { }

Instead doing this works:

MyClass::~MyClass() { }

My question is since I do not have to include virtual in the final code write of the destructor does the destructor still behave as a virtual destructor (since it is virtual as a prototype)?

+12  A: 

The virtual keyword is only used as part of the member function declaration inside of the class definition.

If the member function is defined outside of the class definition, the virtual keyword is not placed there.

James McNellis
That answered my question very nicely :)
Then mark it as answered.
DeadMG
@DeadMG you have to wait 15 minutes before marking an answer.
@m00st: Oh. I've never had a question answered that quickly.
DeadMG
Same for static keyword
Kamil Klimek
+4  A: 

The virtual keyword can only be used on the function declarations within a class declaration (typically in a header file), not on the definitions within a source file. This is true for all functions, not just destructors.

anon