Tuesday 15 June 2010

c++ - Overriding the virtual functions with different return types raises error with private inheritance -



c++ - Overriding the virtual functions with different return types raises error with private inheritance -

in next code got next 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 homecoming type differs , not covariant '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 of operations class 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 homecoming type differs , not covariant '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 of operations class not accessible 1>build log saved @ "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 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 way, fresher c++.. plz help..thanks in advance..

assuming trying utilize covariance of homecoming types, attempts valid except fact types must using public inheritance:

class d1:public base1{ // ~~~~~^ }; class d2:public base1{ // ~~~~~^ }; class _d1 : base2{ public: d1* fun(){} // ok now, d1 inherits publicly base1 }; class _d2 : base2{ public: d2* fun(){} // ok now, d2 inherits publicly base1 };

just can't cast d2* base1* unless using public inheritance, same applies here.

demo

alternatively, have create classes friends, have access private base of operations 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 homecoming type of overriding function shall either identical homecoming type of overridden function or covariant classes of functions. if function d::f overrides function b::f, homecoming types of functions covariant if satisfy next criteria:

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

— class in homecoming type of b::f same class class in homecoming type of d::f, or unambiguous and accessible direct or indirect base of operations class of class in homecoming type of d::f

— both pointers or references have same cv-qualification , class type in homecoming type of d::f has same cv-qualification or less cv-qualification class type in homecoming type of b::f.

c++ return-type method-overriding

No comments:

Post a Comment