Covariant Return type

Trying to understand covariant return types and the following does not seem to work:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
struct IElement
{
  virtual IElement& Get() = 0;
};

template<typename T>
struct Element : public IElement
{
  T data;
  Element(T _data) : data(_data) {}
  Element<T>& Get() { return *this; }
};

int main(void)
{
	Element<float> fE = 5.0f;
	IElement* iE = &fE;
	
	float f = iE->Get().data; //error C2039: 'data' : is not a member of 'IElement'

	return 0;
}



IElement* iE = &fE; The static (compile time) type of the object pointed to is IElement.

Therefore, the static type of iE->Get() is reference to IElement.





Topic archived. No new replies allowed.