C++ 从多个具有相同虚函数名称的基类继承

C++ inherit from multiple base classes with the same virtual function name(C++ 从多个具有相同虚函数名称的基类继承)
本文介绍了C++ 从多个具有相同虚函数名称的基类继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我试过这个代码:

class A
{
    virtual void foo() = 0;
};

class B
{
    virtual void foo() = 0;
};

class C : public A, public B
{
    //virtual void A::foo(){}
    //virtual void B::foo(){}

    virtual void A::foo();
    virtual void B::foo();
};

void C::A::foo(){}
void C::B::foo(){}

int main()
{
    C c;
    return 0;
}

使用注释部分是可以的,但是当我尝试在类声明之外编写定义时,编译器会报错.我正在使用 MSVC11 编译器,有人知道怎么写吗?我需要将代码移动到 cpp 文件中.

It is OK when using the commented part, but when I try to write the definitions outside the class declaration, the compiler reports errors. I am using the MSVC11 compiler, does anyone know how to write this? I need to move the code into the cpp file.

谢谢~~

推荐答案

一个函数根据名称和参数类型覆盖基类的虚函数(见下文).因此,你的类C两个虚函数foo,一个从AB.但是一个函数 void C::foo() 覆盖了 both:

A function overrides a virtual function of a base class based on the name and parameter types (see below). Therefore, your class C has two virtual functions foo, one inherited from each A and B. But a function void C::foo() overrides both:

[class.virtual]/2

[class.virtual]/2

如果在类Base和类Derived中声明了虚拟成员函数vf,直接或间接从派生Base,一个成员函数vf,同名,parameter-type-list,cv-qualification,和ref-qualifier(或没有相同的)作为Base::vf 被声明,然后 Derived::vf 也是虚拟的(无论它是否如此声明)并且它覆盖 Base::vf.

If a virtual member function vf is declared in a class Base and in a class Derived, derived directly or indirectly from Base, a member function vf with the same name, parameter-type-list, cv-qualification, and ref-qualifier (or absence of same) as Base::vf is declared, then Derived::vf is also virtual (whether or not it is so declared) and it overrides Base::vf.

正如我在评论中已经说过的,[dcl.meaning]/1 禁止在(成员)函数的声明中使用 qualified-id:

As I already stated in the comments, [dcl.meaning]/1 forbids the use of a qualified-id in the declaration of a (member) function:

declarator-id 被限定时,该声明应引用该限定符所指的类或命名空间的先前声明的成员 [...]"

When the declarator-id is qualified, the declaration shall refer to a previously declared member of the class or namespace to which the qualifier refers [...]"

因此,任何 virtual void X::foo(); 作为 C 中的声明都是非法的.

Therefore any virtual void X::foo(); is illegal as a declaration inside C.

代码

class C : public A, public B
{
    virtual void foo();
};

是 AFAIK 覆盖 foo 的唯一方法,它会覆盖 A::fooB::foo.除了引入另一层继承之外,没有办法对 A::fooB::foo 进行两个不同的覆盖,并具有不同的行为:

is the only way AFAIK to override foo, and it will override both A::foo and B::foo. There is no way to have two different overrides for A::foo and B::foo with different behaviour other than by introducing another layer of inheritance:

#include <iostream>

struct A
{
    virtual void foo() = 0;
};

struct B
{
    virtual void foo() = 0;
};

struct CA : A
{
    virtual void foo() { std::cout << "A" << std::endl; }
};

struct CB : B
{
    virtual void foo() { std::cout << "B" << std::endl; }
};

struct C : CA, CB {};

int main() {
    C c;
    //c.foo();  // ambiguous

    A& a = c;
    a.foo();

    B& b = c;
    b.foo();
}

这篇关于C++ 从多个具有相同虚函数名称的基类继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Why does C++ compilation take so long?(为什么 C++ 编译需要这么长时间?)
Why is my program slow when looping over exactly 8192 elements?(为什么我的程序在循环 8192 个元素时很慢?)
C++ performance challenge: integer to std::string conversion(C++ 性能挑战:整数到 std::string 的转换)
Fast textfile reading in c++(在 C++ 中快速读取文本文件)
Is it better to use std::memcpy() or std::copy() in terms to performance?(就性能而言,使用 std::memcpy() 或 std::copy() 更好吗?)
Does the C++ standard mandate poor performance for iostreams, or am I just dealing with a poor implementation?(C++ 标准是否要求 iostreams 性能不佳,或者我只是在处理一个糟糕的实现?)