I have following code:
#include <iostream>
using namespace std;
class Child1
{
int i;
};
class Child2 : public Child1
{
int j;
};
class Base1
{
public:
virtual Child1& getChildren()
{
cout << "Children1" << endl;
return children;
}
private:
Child1 children;
}开发者_开发问答;
class Base2 : public Base1
{
public:
virtual Child2& getChildren()
{
cout << "Children2" << endl;
return children;
}
private:
Child2 children;
};
This code compiles fine but when I change the return type of getChildren()
from reference type to object type in either or both Base1
and Base2
(e.g. virtual Child2 getChildren()
, I get the following error on Visual Studio 2010:
error C2555: 'Base2::getChildren': overriding virtual function return type differs and is not covariant from 'Base1::getChildren'
I want to know why am I not getting this error when using reference and getting it otherwise. Is this is a bug in VS2010? Because the C++ standard (according to this page on Microsoft's website) says something like: 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. And 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.
P.S. I do not have access to the standard at the moment so can not verify the above quote.
You missed the other part they quoted: "If a function D::f overrides a function B::f, the return types of the functions are covariant if they satisfy the following criteria: (1) both are pointers to classes or references to classes"
They have to be references or pointers, not concrete classes. That requirements could live quite happily inside the "convariant with" stipulation in your MS quote. The second part speaks about "the class in the return type" - note the care with which they've avoided "the class returned" precisely because it's the class component of the pointer- or reference-to-class return type.
Think about what covariance is for: you're getting back an object and some existing code written for the base class may be wishing to handle that but through virtual dispatch end up getting one of your objects. If they could be different in size how could the objects be stored? This mess is sidestepped by the indirection... you can decide where they are (some buffer, shared mem, heap) and the covariant return type is the pointer or reference thereto.
If you are changing string conversions like std::string to std::wstring or any string conversions of virtual functions, you need to change super class of that virtual function where actually that virtual function is declared. You need to change every where where you are using function in which you are changing string conversions.
In my case that is happened hope it will help some one.. .
精彩评论