"alex goldman" <he***@spamm.er> wrote in message
news:19****************@yahoo.com...
When compiling this code with
g++ -Wall -pedantic -ansi -c program.cpp # this is GCC-3.3.4
######################################
#include <vector>
class b {
public:
virtual void f() = 0;
};
class d : public b {
std::vector<int> v;
public:
void f() {}
};
#####################################
I get a warning:
program.cpp:8: warning: `class d' has virtual functions but non-virtual
destructor
The compiler is clearly telling you that the presence of a pure-virtual
member function in class b does *not* automatically generate a virtual
d~tor. And indeed, it doesn't - thats bad news.
in fact, you should have seen the following warnings with the
-ansi -pedantic -Wall options:
`class b' has virtual functions but non-virtual destructor
`class d' has virtual functions but non-virtual destructor
Am I very confused about the presence of virtual functions in class d, or
is the compiler?
This has nothing to do with the virtual function as per say. The basic rule
in inheritence is that a virtual d~tor should replace the default
non-virtual d~tor in a base class you plan to derive from.
So class B should look like...
class B
{
public:
B() { }
virtual ~B() { }
virtual void f() = 0;
};
class D : public B
{
std::vector<int> vn;
public:
D() : vn() { }
~D() { } // is also virtual since base d~tor is virtual
void f() {} // is also virtual...
};
int main()
{
D d;
system("PAUSE");
return EXIT_SUCCESS;
}
No warning anymore...