Take the 2-minute tour ×

I have the following code:

#include <iostream>

class Grandma
    virtual void foo() = 0;

class Mom : public Grandma{};

class Dad
    void foo() { std::cout << "dad's foo impl"; }

class Me : public Mom, public Dad{};

int main()
    Me m;

and getting : cannot instantiate abstract class

I know what this error means, I know I can't instantiate Grandma because of pure-virtual.

But why can't I instantiate Me, when compiler knows I am derived from Mom and Dad and Dad has foo implemented?

I know I can fixed it by adding foo into Me and inside it call e.g. Dad::foo, but I am afraid it is not solution for my case.

Is it really necessary to have virtual method implementation between its declaration and instantiated object (when traversing "class hierarchy graph")? See ASCI graph

B1  B2
  \ |   
   C1  C2
    | /

When I want to instantiate E and have virtual declaration in A, the only way to make it run is to define it in A, B2, C1, D or E? and similarly when is virtual declaration in C2 only way is to define it in C2, D or E?

I know this may be silly question, but I had a loooong day and can not think anymore.

Please, do not answer just with - "It is not possible", but try to add explanation why not.

Thank you!

EDIT -- foo() in Dad is of course should not be private

share|improve this question
your Dad class doesn't derive from Grandma class, so Dad::foo doesn't override base class pure virtual function. Additionally, your derivation is not virtual, so you have two separate subobjects in Me –  Gene Bushuyev Aug 4 '11 at 17:22
@Gene Bushuyev There is no diamond inheritance here so virtual inheritance should have no effect. –  Mark B Aug 4 '11 at 17:30
@Mark -- that's correct, but that wasn't my point. If the Dad were to derive from Grandma, there would have been a diamond, and the same error, making the derivation virtual would have solved the latter. –  Gene Bushuyev Aug 4 '11 at 17:42
Yeah, dad should have a mom, thus be derived from grandma.. Perhaps just bad names for an example :) –  Vlad Lazarenko Aug 4 '11 at 19:16
add comment

3 Answers

up vote 4 down vote accepted

There is no relation between Dad class and Grandma class. Dad and Grandma are two completely different classes. Given that the method in Dad is not considered as an implementation of the pure virtual method in Grandma and has to be implemented in the Me class.

When you derive from an Abstract class(class containing atleast one pure virtual function) the deriving class needs to override and implement ALL the pure virtual member functions of the Base Class. If not, the deriving class becomes Abstract too, same applies to the classes deriving from the derived class further down the hierarchy.

share|improve this answer
Would adding using Dad::foo to Me be sufficient to meet this condition? –  rcollyer Aug 4 '11 at 17:33
Your answer would be clearer if you avoided using a valid operator as an abbreviation for the word and –  Nicola Musatti Aug 4 '11 at 17:34
@Nicola Musatti: Do you mean replacing & with a and, And did you really downvote for this reason? –  Alok Save Aug 4 '11 at 17:39
I would really like to know the reason for this downvote. –  Alok Save Aug 4 '11 at 17:41
@Nicola Musatti: I find it amusing that that you would downvote someone's answer because You did not understand it. I am glad though OP understood it and asked the Q's which indicated so. :) –  Alok Save Aug 4 '11 at 18:43
show 13 more comments

Unfortunately you're right that you have to implement the virtual method in the path between original declaration and final instantiated object. Objects multiply inherited in can't implement that interface.

So from the question it sounds like you're trying to implement a parent interface in a child class by inheriting from the implementor. Instead of that approach, did you consider instead having the parent class delegate the work to a strategy pattern and then select the proper strategy in your child class. Then you can still compose implementations together and don't have to worry about the many complexities of multiple inheritance.

share|improve this answer
add comment

You are correct that a virtual function is only overridden by implementations provided by classes inheriting (potentially through several layers) from the class where the virtual function is originally declared.

However, depending on what you are actually trying to accomplish, Sister Class Delegation may what you want. Basically, you can declare virtual functions in a base common to both Grandma and Dad ('Person'?) and Grandma can call those functions even if only Dad provides an implementation when you're using a Me object. Even though Grandma would know nothing about Dad, when you use virtual inheritance any subclasses your final class pulls in can be used to satisfy virtual functions from the virtual base class.

share|improve this answer
add comment

Your Answer


