其他分享
首页 > 其他分享> > c – 如何覆盖另一个基类中的函数?

c – 如何覆盖另一个基类中的函数?

作者:互联网

我不确定要使用的术语,但这是我的例子:

class Base {
public:
    virtual void test() = 0;
};

class Mixin {
public:
    virtual void test() { }
};

class Example : public Base, public Mixin {
};

int main(int argc, char** argv) {
    Example example;
    example.test();
    return 0;
}

我希望我的Mixin类实现纯虚函数Base :: test,但是当我编译它时,它说:

test.cpp: In function ‘int main(int, char**)’:
test.cpp:15:13: error: cannot declare variable ‘example’ to be of abstract type ‘Example’
     Example example;
             ^
test.cpp:11:7: note:   because the following virtual functions are pure within ‘Example’:
 class Example : public Base, public Mixin {
       ^
test.cpp:3:18: note:    virtual void Base::test()
     virtual void test() = 0;
                  ^
test.cpp:16:13: error: request for member ‘test’ is ambiguous
     example.test();
             ^
test.cpp:8:18: note: candidates are: virtual void Mixin::test()
     virtual void test() { }
                  ^
test.cpp:3:18: note:                 virtual void Base::test()
     virtual void test() = 0;
                  ^

我可以添加一个using语句,使其不含糊:

class Example : public Base, public Mixin {
public:
    using Mixin::test;
};

但它说我还没有实现它:

test.cpp: In function ‘int main(int, char**)’:
test.cpp:17:13: error: cannot declare variable ‘example’ to be of abstract type ‘Example’
     Example example;
             ^
test.cpp:11:7: note:   because the following virtual functions are pure within ‘Example’:
 class Example : public Base, public Mixin {
       ^
test.cpp:3:18: note:    virtual void Base::test()
     virtual void test() = 0;
                  ^

是否有可能做到这一点?

我知道一个选项是让Mixin从Base继承,但在我的情况下,有几个派生类,它们不共享一个共同的祖先.

解决方法:

您不能直接让类覆盖不是其基类的方法.但是你可以用迂回的方式来做.我将介绍两种这样的方法 – 我更喜欢第二种方法.

方法1

Daniel Paul在thinkbottomup.com.au的一篇文章中描述了这一点,题为C++ Mixins – Reuse through inheritance is good… when done the right way.

在你的情况下,这是它的样子:

class Base {
public:
    virtual void test() = 0;
};

template <typename T>
class Mixin : public T {
public:
    virtual void test() override { /*... do stuff ... */ }
};

class UnmixedExample : public Base {
    /* definitions specific to the Example class _not_including_
       a definition of the test() method */
};

using Example = class Mixin<UnmixedExample>;

int main(int argc, char** argv) {
    Example{}.test();
    return 0;
}

方法2:CRTP!

CRTP是“奇怪的重复模板模式” – 如果您之前没有看过它,请务必遵循该链接.使用这种方法,我们将使用虚拟继承说明符来避免歧义,与前一种方法不同 – 我们不会颠倒Mixin和Example类的继承顺序.

class Base {
public:
    virtual void test() = 0;
};

template <typename T>
class Mixin : virtual T {
public:
    virtual void test() override { /*... do stuff ... */ }
};

class Example : public virtual Base, public virtual Mixin<Base> {
    /* definitions specific to the Example class _not_including_
       a definition of the test() method */
};

int main(int argc, char** argv) {
    Example{}.test();
    return 0;
}

注意两种解决方案:

>好奇CRTP是如何在整个地方重复出现的?

标签:c,inheritance,multiple-inheritance
来源: https://codeday.me/bug/20191008/1870840.html