用不同的返回类型覆盖虚拟函数会引发私有继承错误 [英] Overriding the virtual functions with different return types raises error with private inheritance
问题描述
在以下代码中,出现以下编译错误:
In the following code I got the following compilation errors:
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 ++更加了解.
By the way, I am fresher to C++.. plz help..thanks in advance..
推荐答案
假设您尝试使用 返回类型的协方差 ,您的尝试是有效的,但必须使用公共继承:
Assuming you are trying to use covariance of return types, your attempts are valid except for the fact that the types must be using a public inheritance:
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*
一样,除非您使用的是公共继承,此处也是如此.
Just like you can't cast D2*
to Base1*
unless you are using a public inheritance, the same applies here.
或者,您必须使这些类成为朋友,以便他们可以访问到私有基类:
Alternatively, you would have to make those classes to be friends, so that they have access to the private base class:
class _D1;
class _D2;
class D1 : Base1{
friend class _D1;
};
class D2 : Base1{
friend class _D2;
};
C ++标准参考:
C++ Standard reference:
§10.3虚拟函数
[class.virtual]
§ 10.3 Virtual functions
[class.virtual]
-
覆盖函数的返回类型应与覆盖函数的返回类型相同或与函数的类协变.如果函数
D::f
覆盖函数B::f
,则如果函数的返回类型满足以下条件,则它们是协变的:
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 functionB::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
-返回类型为B::f
的类与返回类型为D::f
的类相同,或者是明确的且可访问的直接或间接基类.返回类型为D::f
— 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
-指针或引用都具有相同的cv限定,并且D::f
的返回类型中的类类型具有与B::f
的返回类型中的类类型相同或更少的cv限定. .
— 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
.
这篇关于用不同的返回类型覆盖虚拟函数会引发私有继承错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!