Why does the linker give me an error in the virtual case but not in the non-virtual case?
c++, inheritance, virtual
Solution
When both classes define virtual functions, C++ compiler needs to build vtables for both classes `A` and `B`. To build `A`'s vtable, the compiler needs `A::rat()` - that is where the reference is coming from.
When `A` has no virtual functions, there are no reference to `A::rat` from anywhere, hence you do not get a compile error.
As I'm sure you know, you can fix this error by making `A::rat` a pure virtual, thus providing the needed value for the vtable (in this case, the value is zero).
Problem
When I have something like this: ``` class A { virtual void rat(); }; class B : public A { virtual void rat() { ; } //implemented! }; int main(int argc, char **argv) { A *a = new B; delete a; } ``` I get linker errors: unless I make the base rat a pure virtual. However, when I have this: ``` class A { public: void rat(); }; int main(int argc, char **argv) { A a; } ``` This compiles fine and doesn't give me an undefined reference link error unless I explicitly try to call the rat function in my main (`a.rat();`). What is the rule on unimplemented base class virtual functions which are, however, implemented in a derived class as in the first failing code snippet?