-1

Suppose I override a virtual function in a child class with a declaration, and do not give a definition for the method. For example:

class Base
{
    virtual void f() = 0;
}
class Derived : public Base
{
    void f();
}

(where I haven't given the definition of f). If I now use the class Derived - is it possible that i get a compiler error like "undefined reference to vtable..."?

yizzlez
  • 8,757
  • 4
  • 29
  • 44
  • You would certainly get an error if your code ever attempts to reference/call `Derived.f()`, but I think it would be a different one. "undefined reference to vtable" is, IIRC, usually a linker error, meaning that you are not including the object file or library containing the vtable for that class in your link stage... – twalberg Apr 23 '14 at 15:37
  • Why don't you compile it and tell us? – RamblingMad Apr 24 '14 at 01:06

1 Answers1

0

Not for f. You've explicitly said that the function will not have an implementation, which implies that derived classes must implement it.

However, if you declare a pure virtual destructor (e.g., virtual ~Base()= 0), you will have to provide a definition somewhere. In that case, if you don't, you will get a "undefined reference to vtable..." error.

MSN
  • 53,214
  • 7
  • 75
  • 105