简体   繁体   English

派生类中的C++纯虚函数实现

[英]C++ pure virtual functions implementation in derived class

I have a top API (based on abstract class) which is composed by a base API (that will be reused by different top APIs).我有一个顶级API(基于抽象类),它由一个基本API(将由不同的顶级API 重用)组成。

class api_base {
public:
    virtual int foo() = 0;
};

class api_top : public api_base {
public:
    virtual int bar() = 0;
}

Then, I want to provide a base implementation for base API:然后,我想为基本 API 提供一个基本实现:

class imp_base {
public:
    int foo() { return 1; }
}

And finally implement top API using base implementation for those functions defined in base API:最后使用基本 API 中定义的函数的基本实现来实现顶级API:

class imp_top : public api_top, public imp_base {
public:
    int bar() { return 1; }
}

When I instantiate an object of imp_top type, compiler say that foo() function is not implemented.当我实例化一个imp_top类型的对象时,编译器说foo()函数没有实现。 But that is not true, since imp_top derives from imp_base , which do have foo() function implemented.但事实并非如此,因为imp_top派生自imp_base ,它确实实现了foo()函数。

Any advice about this?对此有何建议?

Thank you in advance.先感谢您。

Full test code:完整的测试代码:

    #include <stdio.h>

    class api_base { 
    public:
        virtual int foo() = 0;
    };

    class api_top : public api_base { 
    public:
        virtual int bar() = 0;
    };

    class imp_base { 
    public:
        int foo() { printf("foo from imp_base\n"); } 
    };

    class imp_top : public api_top, public imp_base { 
    public:
        int bar() { printf("bar from imp_top\n"); } 
    };

    int main()
    { 
       printf("Hello\n");                                                                                                                                                                                                                                                   
       imp_top a;

       a.bar();
       a.foo();

       return 1;
    }

Compiler result:编译结果:

    test.cpp:26:12: error: cannot declare variable ‘a’ to be of abstract type ‘imp_top’                                                                                                                                                                                     
        imp_top a;
                ^
    test.cpp:18:7: note:   because the following virtual functions are pure within ‘imp_top’:
     class imp_top : public api_top, public imp_base {
           ^
    test.cpp:5:17: note:  virtual int api_base::foo()
         virtual int foo() = 0;
                     ^
    test.cpp:29:6: error: request for member ‘foo’ is ambiguous
        a.foo();
          ^
    test.cpp:15:9: note: candidates are: int imp_base::foo()
         int foo() { printf("foo from imp_base\n"); }
             ^
    test.cpp:5:17: note:                 virtual int api_base::foo()
         virtual int foo() = 0;

First Always use override keyword when redefining the functions in child classes.首先在子类中重新定义函数时总是使用override关键字。

Second read about virtual inheritance.第二次阅读虚拟继承。

The following works :以下工作:

class api_base {
public:
    virtual int foo() = 0;
};

class api_top : public virtual api_base {
public:
    virtual int bar() = 0;
};

class imp_base : public virtual api_base {
public:
    int foo() override { printf("foo from imp_base\n"); return 0; }
};

class imp_top : public api_top, public imp_base {
public:
    int bar() override { printf("bar from imp_top\n"); return 0; }
};


int main(){
    imp_top a;

    a.bar();
    a.foo();
return 1;
}

You should have used the override keyword, then you would had noticed that you did not implement the interface but defined an entirely new foo() method.您应该使用override关键字,然后您会注意到您没有实现接口而是定义了一个全新的foo()方法。

You will need to derive both imp_base and api_top from api_base by virtual inheritance.您需要通过虚拟继承从api_base派生imp_baseapi_top

Change:改变:

class api_top : public api_base to class api_top : public virtual api_base class api_top : public api_baseclass api_top : public virtual api_base

and:和:

class imp_base to class imp_base : public virtual api_base class imp_baseclass imp_base : public virtual api_base

Then it works.然后它起作用了。

To understand this, see: virtual inheritance .要理解这一点,请参阅:虚拟继承 And yes (just saw Ext3h's post), use the override keyword.是的(刚刚看到 Ext3h 的帖子),使用override关键字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM