R
Rob Grainger
Hi,
When compiling the following code under VC .NET,
template <typename T>
class IBaseCollection
{
public:
virtual T Get() const = 0;
};
template <typename T, typename Base>
class IDerivedCollection : public Base
{
public:
virtual T Get() const = 0;
};
class Element
{
public:
virtual Element* Get() = 0;
virtual IBaseCollection<Element*>& GetCollection() = 0;
};
class OwnedElement : public Element
{
public:
virtual OwnedElement* Get() = 0;
virtual IDerivedCollection<OwnedElement*, IBaseCollection<Element*> >&
GetCollection() = 0;
};
I receive the following compile error:
c:\Documents and Settings\Administrator\My Documents\Visual Studio
Projects\CollTest\CollTest.cpp(19) : error C2555:
'IDerivedCollection<T,Base>::Get': overriding virtual function return type
differs and is not covariant from 'IBaseCollection<T>::Get'
with
[
T=OwnedElement *,
Base=IBaseCollection<Element *>
]
and
[
T=Element *
]
c:\Documents and Settings\Administrator\My Documents\Visual Studio
Projects\CollTest\CollTest.cpp(11) : see declaration of
'IBaseCollection<T>::Get'
with
[
T=Element *
]
c:\Documents and Settings\Administrator\My Documents\Visual Studio
Projects\CollTest\CollTest.cpp(35) : see reference to class template
instantiation 'IDerivedCollection<T,Base>' being compiled
with
[
T=OwnedElement *,
Base=IBaseCollection<Element *>
]
In fact, the return type of Get with the parameters given is Element* for
IBaseCollection<Element*> and OwnedElement*
for IDerivedCollection<OwnedElement*, IBaseCollection<Element*>. According
to ANSI C++, these should be covaraint types.
Does anyone know any work arounds, or something I'm missing here,
Thanks,
Rob
When compiling the following code under VC .NET,
template <typename T>
class IBaseCollection
{
public:
virtual T Get() const = 0;
};
template <typename T, typename Base>
class IDerivedCollection : public Base
{
public:
virtual T Get() const = 0;
};
class Element
{
public:
virtual Element* Get() = 0;
virtual IBaseCollection<Element*>& GetCollection() = 0;
};
class OwnedElement : public Element
{
public:
virtual OwnedElement* Get() = 0;
virtual IDerivedCollection<OwnedElement*, IBaseCollection<Element*> >&
GetCollection() = 0;
};
I receive the following compile error:
c:\Documents and Settings\Administrator\My Documents\Visual Studio
Projects\CollTest\CollTest.cpp(19) : error C2555:
'IDerivedCollection<T,Base>::Get': overriding virtual function return type
differs and is not covariant from 'IBaseCollection<T>::Get'
with
[
T=OwnedElement *,
Base=IBaseCollection<Element *>
]
and
[
T=Element *
]
c:\Documents and Settings\Administrator\My Documents\Visual Studio
Projects\CollTest\CollTest.cpp(11) : see declaration of
'IBaseCollection<T>::Get'
with
[
T=Element *
]
c:\Documents and Settings\Administrator\My Documents\Visual Studio
Projects\CollTest\CollTest.cpp(35) : see reference to class template
instantiation 'IDerivedCollection<T,Base>' being compiled
with
[
T=OwnedElement *,
Base=IBaseCollection<Element *>
]
In fact, the return type of Get with the parameters given is Element* for
IBaseCollection<Element*> and OwnedElement*
for IDerivedCollection<OwnedElement*, IBaseCollection<Element*>. According
to ANSI C++, these should be covaraint types.
Does anyone know any work arounds, or something I'm missing here,
Thanks,
Rob