Pages: 12
Is this an acceptable solution?

struct I
   virtual void foo() = 0;

void I::foo() {}

struct J : I {};  // error! does not override I::foo
struct K: I
   virtual void foo() override
      I::foo();   // invokes "default" definition
No. I::foo() is a pure virtual function. It has no implementation, so you can't call it.

struct J : I {};  // error! does not override I::foo 
That's not an error. A class is not required to implement all pure virtual functions of its base classes. It's only required to do so if it's instantiated.
Last edited on
helios, the implementation to I::foo() is on line 6.

I don't know what denver wants as a "solution", because I don't know what we're trying to solve, but the code above is at least legal.
Last edited on
> Is this an acceptable solution?

It would compile cleanly; K is not an abstract class.
My bad, I didn't see it.
All I am trying to figure out here is that if its a MUST to override and overload a pure virtual function.
This is going to keep going in circles :)
(1) This discussion is not about function overloading.
(2) A pure virtual function must be overridden and implemented in a subclass, if you wish to be able to instantiate that subclass as an object; otherwise, the class is abstract.
Last edited on
the implementation to I::foo() is on line 6.

How can that be legal when line 3 says that I::foo() is pure virtual? Isn't a definition of I::foo() illegal at that point?
Yeah, ain't it a funny thing? I didn't know this either, but apparently all the "= 0" dictates is that the subclass must override the function, if you wish for the class to not be abstract. It doesn't prevent the abstract class itself from having an implementation for the function.
It's such a stupid feature. Just define a separate function for the implementation in the subclass to call!
Scott Meyers in 'Effective C++'
Derived classes that implement this pure virtual function may call this implementation somewhere in their code. If part of the code of two different derived classes is similar then it makes sense to move it up in the hierarchy, even if the function should be pure virtual.
denver2020 wrote:
All I am trying to figure out here is that if its a MUST to override [...] a pure virtual function.

If you want to instantiate a class, then that class must have an implementation of all the functions that were declared pure virtual in an inherited class. This means that, yes, you need to override those pure virtual functions.

and overload

You have been told again and again that you do not need to overload anything, and that overloading is irrelevant to a discussion of pure virtual functions. Why are you not listening?
Topic archived. No new replies allowed.
Pages: 12