问题
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)
Here is the code:
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(){}
};
By the way, I am fresher to C++.. plz help..thanks in advance..
回答1:
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
};
Just like you can't cast D2*
to Base1*
unless you are using a public inheritance, the same applies here.
DEMO
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++ Standard reference:
§ 10.3 Virtual functions
[class.virtual]
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
— the class in the return type of
B::f
is the same class as the class in the return type ofD::f
, or is an unambiguous and accessible direct or indirect base class of the class in the return type ofD::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 ofB::f
.
回答2:
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 or 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 direct or indirect base class of the class in the return type of D::f and is accessible in D
-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.
回答3:
You must change all the fun()
s to return Base1
.
来源:https://stackoverflow.com/questions/26354807/overriding-the-virtual-functions-with-different-return-types-raises-error-with-p