Overriding virtual functions with different return types throws a private inheritance error
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(){}
};
BTW I'm fresh 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 use 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*
before Base1*
, if you don't use public inheritance then this also applies here.
DEMO
Also, you will need to make these classes friendly so that they can access 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 the overridden function must be either identical to the return type of the overridden function, or covariantly with the function classes. If a function
D::f
overrides a functionB::f
, the return types of the functions are covariant if they meet the following criteria:- both are class pointers, both are lvalue references for classes, or both are rvalue references for classes
- the class of the return type
B::f
is the same class as the class of the return typeD::f
, or is an unambiguous and accessible direct or indirect base class the class in the return typeD::f
- both pointers or references have the same cv-qualification and the class type in the return type
D::f
has the same cv-qualification as or less cv-qualification than the class type in the return typeB::f
.
The return type of the overridden function must be either identical to the return type of the overridden function, or covariant with the function classes. If the function D :: f overrides the function B :: f, the return types of the functions are covariant if they meet the following criteria:
- both are class pointers or class references the
class in the return type B :: f is the same class as the return type class D :: f or, is the unambiguous direct or indirect base class of the class in the return type D :: f, and is available in D - only
pointers or references have the same cv-qualification, and the class type in the return type D :: f has the same cv-qualification as or less cv-qualification than the class type in the return type B :: f.
You must change everything fun()
in order to return Base1
.