Should I declare f () explicitly in B?
Yes , you must declare in the class definition the entire virtual function of any base classes that you want to override in the class. As for why: This is just a C ++ syntax way.
Note that the virtual may be omitted to declare overridden virtual functions:
class base { virtual void f(); virtual void g(); }; class derived : public base { virtual void f();
Note. Class declaration: class my_class; , and this is class my_class { /* ... */ }; - class definition. There is a limited number of things you can do with a class that has been declared but not defined. In particular, you cannot instantiate or call member functions.
See here for more details on the differences between declarations and definitions.
Well, in the interest of discussing the “definition versus definition” occurring in the comments, here is a quote from the C ++ 03 standard, 3.1 / 2:
A declaration is a definition if it is [...] a declaration of the class name [...].
3.1 / 3 gives some examples. Among them:
[Example: [...]
struct S { int a; int b; }; // defines S, S::a, and S::b
[...]
struct S; // declares S
-end example]
To summarize: the C ++ standard considers struct S; like declaration and struct S { /*...*/ }; definition. I find this a strong backup of my interpretation of the declaration versus definition for C ++ classes.
source share