Suppose I have the following code:
class Iinterface
{
virtual void abstractFunction()=0;
};
class Derived : public Iinterface
{
void abstractFunction(); // Do I need this line?
};
Derived::abstractFunction()
{
// implementation here
}
If I don’t add the line in question, I get compile error which says abstractFunction is not declared in Derived. I’m using VS 2008.
I’m not sure why I need this particular line (do not confuse this with the function definition which is provided outside class declaration), as long as I’m inheriting from Iinterface it should be obvious I have abstractFunction declared. Is that a problem with visual studio or is it enforced by c++ standards?
If the declaration of pure-virtual base functions were implied in all derived classes, then you could never have a derived class that remains abstract with respect to a pure-virtual base function. Instead, all derived classes would produce linker errors. That would be extremely counter-intuitive and confusing, and it would make the language less expressive.
Moreover, it wouldn’t even make sense: The question whether the derived class is abstract or not must be known everywhere at compile-time. The implementation of the overrider is typically only provided in one single translation unit, so it would be impossible to communicate the fact that you actually mean for the function to be overridden to the rest of the program.