invalid covariant return type

  • Thread starter Sebastian Schucht
  • Start date
S

Sebastian Schucht

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<TType> from 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<TType> operator+(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::eek:perator+(const A<C>&)'
mini.cpp:8: error: overriding 'A<TType> A<TType>::eek:perator+(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
 
K

Kai-Uwe Bux

Sebastian said:
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<TType> from 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<TType> operator+(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::eek:perator+(const A<C>&)'
mini.cpp:8: error: overriding 'A<TType> A<TType>::eek:perator+(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
 
V

Victor Bazarov

Sebastian said:
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<TType> from 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<TType> operator+(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::eek:perator+(const A<C>&)'
mini.cpp:8: error: overriding 'A<TType> A<TType>::eek:perator+(const
A<TType>&) [with TType = C]'

- Why is this an error?

A<C> is not covariant with B, they are not the same type. A<C>& would
be covariant with B&. A said:
- 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
 
A

Alf P. Steinbach

* Victor Bazarov:
"Covariant" is either a pointer or a reference. With objects, they
have to be _exactly the same_ to be covariant.

You mean, the have to be exactly the same.

When the type is the same there is no variance.

Cheers, & hth.,

- Alf
 
A

Alf P. Steinbach

* Victor Bazarov:
"Covariant" is either a pointer or a reference. With objects, they
have to be _exactly the same_ to be covariant.

You mean, the have to be exactly the same.

When the types are the same, there is no variance.

Cheers, & hth.,

- Alf
 
A

Alf P. Steinbach

* Sebastian Schucht:
I have a templateclass with [pure] virtual member-functions, so i can't create
instances ... but for returning the result i would like use one. So i
replaced the A<TType> from 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<TType> operator+(const A<TType>& i_sum) = 0;
};

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

Depends what your intent is.

You could do something like

class B: public A<C>
{
public:
B( A<C> const& ) {}

B operator+( B const& ) { return *this; }

virtual A<C> operator+( A<C> const& x )
{ return operator+( B(x) ); }
}

but that involves a slice for the function result.

Most probably what you really want is simply to support '+', and in that
case you don't want the virtual mechanism more than you want it for '='.

Possibly with this input you'll understand your design better so that
you can express whatever it is you want.


Cheers, & hth.,

- Alf
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,077
Latest member
SangMoor21

Latest Threads

Top