When I try to compile this:
Code:
class Base {
public:
virtual void functionA() {cout<<"Base::functionA"<<endl;}
virtual void functionB(){cout<<"Base::functionB"<<endl;}
virtual ~Base();
};
class Derived1: public Base {
public:
virtual void functionA() {cout<<"Derived1::functionA"<<endl;}
};
It does not compile, saying:
Code:
undefined reference to `vtable for Base'
undefined reference to `Base::~Base()'
However, when I change the above to this:
Code:
class Base {
public:
virtual void functionA() {cout<<"Base::functionA"<<endl;}
virtual void functionB(){cout<<"Base::functionB"<<endl;}
virtual ~Base() {} // notice empty implementation added
};
class Derived1: public Base {
public:
virtual void functionA() {cout<<"Derived1::functionA"<<endl;}
};
It compiles just fine.
Why must a virtual function be implemented, but non virtual function can stay unimplemented as long as you don't call them, of course...