gpt4 book ai didi

c++ - 覆盖具有不同返回类型的虚函数会引发私有(private)继承错误

转载 作者:太空狗 更新时间:2023-10-29 20:58:50 26 4
gpt4 key购买 nike

在下面的代码中我得到了以下编译错误:

1>c:\users\mittamani\desktop\06-10\over_riding_test\over_riding_test\over_riding_test.cpp(33) : error C2555: '_D1::fun': overriding virtual function return type differs and is not covariant from 'Base2::fun'
1> c:\users\mittamani\desktop\06-10\over_riding_test\over_riding_test\over_riding_test.cpp(28) : see declaration of 'Base2::fun'
1> 'Base1' : base class is not accessible
1>c:\users\mittamani\desktop\06-10\over_riding_test\over_riding_test\over_riding_test.cpp(37) : error C2555: '_D2::fun': overriding virtual function return type differs and is not covariant from 'Base2::fun'
1> c:\users\mittamani\desktop\06-10\over_riding_test\over_riding_test\over_riding_test.cpp(28) : see declaration of 'Base2::fun'
1> 'Base1' : base class is not accessible
1>Build log was saved at "file://c:\Users\mittamani\Desktop\06-10\Over_riding_Test\Over_riding_Test\Debug\BuildLog.htm"
1>Over_riding_Test - 2 error(s), 0 warning(s)

代码如下:

class Base1{
public:
Base1(){}
virtual ~Base1(){}
};

class D1:Base1{

};

class D2:Base1{

};

class Base2{
public:
Base2(){}
virtual ~Base2(){}
virtual Base1 * fun() = 0;
};

class _D1:Base2{
public:
D1* fun(){}
};

class _D2:Base2{
public:
D2* fun(){}
};

顺便说一句,我对 C++ 比较陌生..请帮助..提前致谢..

最佳答案

假设您正在尝试使用 covariance of return types ,除了类型必须使用公共(public)继承这一事实外,您的尝试是有效的:

class D1:public Base1{
// ~~~~~^

};

class D2:public Base1{
// ~~~~~^
};

class _D1 : Base2{
public:
D1* fun(){} // ok now, D1 inherits publicly from Base1
};

class _D2 : Base2{
public:
D2* fun(){} // ok now, D2 inherits publicly from Base1
};

就像你不能将 D2* 转换为 Base1* 除非你使用公共(public)继承,这同样适用于这里。

DEMO


或者,您必须使这些类成为 friend ,以便它们可以访问私有(private)基类:

class _D1;
class _D2;

class D1 : Base1{
friend class _D1;
};

class D2 : Base1{
friend class _D2;
};

C++ 标准引用:

§ 10.3 Virtual functions [class.virtual]

  1. The return type of an overriding function shall be either identical to the return type of the overridden function or covariant with the classes of the functions. If a function D::f overrides a function B::f, the return types of the functions are covariant if they satisfy the following criteria:

    — both are pointers to classes, both are lvalue references to classes, or both are rvalue references to classes

    — the class in the return type of B::f is the same class as the class in the return type of D::f, or is an unambiguous and accessible direct or indirect base class of the class in the return type of D::f

    — both pointers or references have the same cv-qualification and the class type in the return type of D::f has the same cv-qualification as or less cv-qualification than the class type in the return type of B::f.

关于c++ - 覆盖具有不同返回类型的虚函数会引发私有(private)继承错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26354807/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com