By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,494 Members | 892 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,494 IT Pros & Developers. It's quick & easy.

invalid covariant return type

P: n/a
Hi,

I have a templateclass with virtual member-functions, so i can't create
instances ... but for returning the result i would like use one. So i
replaced the A<TTypefrom the baseclass with the Type from the spezialized
class. After this, the error invalid covariant return type occoured.

the following minimal sample shows the problem:

#include <iostream>
class C{};

template<class TType>
class A
{
public:
virtual A<TTypeoperator+(const A<TType>& i_sum) = 0;
};

class B:public A<C>
{
public:
B operator+(const A<C>& i_sum) {return *this;};
};

main()
{
B a,b,c;
a+b;
}

After compiling with g++ version 4.1.2 (Ubuntu 4.1.2-0ubuntu4) i have the
following erros on screen:

mini.cpp:14: error: invalid covariant return type for 'virtual B
B::operator+(const A<C>&)'
mini.cpp:8: error: overriding 'A<TTypeA<TType>::operator+(const
A<TType>&) [with TType = C]'

- Why is this an error?
- On wich way i can fix it?

Thank you for your help!

With best regards

Sebastian Schucht

--
B.Sc. Sebastian Schucht
TeichhausstraƟe 38
64287 Darmstadt

UIN: 260 121 043
skype: SebastianSchucht
Dec 6 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Sebastian Schucht wrote:
I have a templateclass with virtual member-functions, so i can't
create instances ... but for returning the result i would like use
one. So i replaced the A<TTypefrom the baseclass with the Type from
the spezialized class. After this, the error invalid covariant return
type occoured.
"Covariant" is either a pointer or a reference. With objects, they
have to be _exactly the same_ to be covariant.
the following minimal sample shows the problem:

#include <iostream>
class C{};

template<class TType>
class A
{
public:
virtual A<TTypeoperator+(const A<TType>& i_sum) = 0;
};

class B:public A<C>
{
public:
B operator+(const A<C>& i_sum) {return *this;};
};

main()
{
B a,b,c;
a+b;
What's the 'c' object for?
}

After compiling with g++ version 4.1.2 (Ubuntu 4.1.2-0ubuntu4) i have
the following erros on screen:

mini.cpp:14: error: invalid covariant return type for 'virtual B
B::operator+(const A<C>&)'
mini.cpp:8: error: overriding 'A<TTypeA<TType>::operator+(const
A<TType>&) [with TType = C]'

- Why is this an error?
A<Cis not covariant with B, they are not the same type. A<C>& would
be covariant with B&. A<C>* would be covariant with B*.
- On wich way i can fix it?
You can't. Don't make operators virtual, it's unnatural since they
return objects (where slicing will most likely occur).

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Dec 6 '07 #2

P: n/a
Sebastian Schucht wrote:
Hi,

I have a templateclass with virtual member-functions, so i can't create
instances ... but for returning the result i would like use one. So i
replaced the A<TTypefrom the baseclass with the Type from the
spezialized class. After this, the error invalid covariant return type
occoured.

the following minimal sample shows the problem:

#include <iostream>
class C{};

template<class TType>
class A
{
public:
virtual A<TTypeoperator+(const A<TType>& i_sum) = 0;
};

class B:public A<C>
{
public:
B operator+(const A<C>& i_sum) {return *this;};
};

main()
{
B a,b,c;
a+b;
}

After compiling with g++ version 4.1.2 (Ubuntu 4.1.2-0ubuntu4) i have the
following erros on screen:

mini.cpp:14: error: invalid covariant return type for 'virtual B
B::operator+(const A<C>&)'
mini.cpp:8: error: overriding 'A<TTypeA<TType>::operator+(const
A<TType>&) [with TType = C]'

- Why is this an error?
Because of clause [10.3/10]:
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. If a function D::f overrides a function B::f, the return
types of the functions are covariant if they satisfy the following
criteria:
? both are pointers to classes or references to classes
...

As you can see, it only works for pointers or references.

- On wich way i can fix it?
Return a reference.

Now, that will very likely not work for operator+ (if it does something
remotely similar to adding things). However, you could do it for operator+=
because that has a reasonable claim to return *this.

Better advice might be possible if you provide some background as to what
the underlying problem is that led you to consider a virtual operator+ in
the first place.
Best

Kai-Uwe Bux
Dec 7 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.