开发者

Implementing virtual methods in other parent-branch than they are declared in

开发者 https://www.devze.com 2023-03-26 11:43 出处:网络
I have the following code: #include <iostream> class Grandma { public: virtual void foo() = 0; }; class Mom : public Grandma{};

I have the following code:

#include <iostream>

class Grandma
{
public:
    virtual void foo() = 0;
};

class Mom : public Grandma{};

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

class Me : public Mom, public Dad{};

int main()
{
    Me m;
    m.foo();
}

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

    A
    |
B1  B2
  \ |   
   C1  C2
    | /
    |/
    D
    |
    E

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


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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消