Because the C++ standard requires it to be implemented. From C++03 §10.3/8:
A virtual function declared in a class shall be defined, or declared pure (10.4) in that class, or both; but no diagnostic is required (3.2).
So you need to either declare it pure (with the = 0
suffix after the right parenthesis) or define its implementation.
As for why you don't get an error when you don't call the function in the non-virtual case, refer to C++03 §3.2/2-3 (emphasis mine)
2) An expression is potentially evaluated unless it appears where an integral constant expression is required
(see 5.19), is the operand of the sizeof
operator (5.3.3), or is the operand of the typeid
operator and
the expression does not designate an lvalue of polymorphic class type (5.2.8). An object or non-overloaded
function is used if its name appears in a potentially-evaluated expression. A virtual member function is
used if it is not pure. [...]
3) Every program shall contain exactly one definition of every non-inline function or object that is used in that
program; no diagnostic required. The definition can appear explicitly in the program, it can be found in the
standard or a user-defined library, or (when appropriate) it is implicitly defined (see 12.1, 12.4 and 12.8).
An inline function shall be defined in every translation unit in which it is used.
So, in the non-virtual case, if it's not used, a definition is not required. But in the non-pure virtual case, even if it's not explicitly referenced in code, it's still considered to be used by the standard, so its definition is required.
See also A virtual member function is used if it is not pure?.