invalid covariant return type



 DEVELOP > c-Plus-Plus > invalid covariant return type

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "Sebastian Schucht"
Date: 06 Dec 2007 05:26:06 PM
Object: invalid covariant return type
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::operator+(const A<C>&)'
mini.cpp:8: error: overriding 'A<TType> A<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
.

User: "Alf P. Steinbach"

Title: Re: invalid covariant return type 06 Dec 2007 06:35:25 PM
* 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
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
.

User: "Kai-Uwe Bux"

Title: Re: invalid covariant return type 06 Dec 2007 05:51:37 PM
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<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::operator+(const A<C>&)'
mini.cpp:8: error: overriding 'A<TType> A<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
.

User: "Victor Bazarov"

Title: Re: invalid covariant return type 06 Dec 2007 05:53:49 PM
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<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::operator+(const A<C>&)'
mini.cpp:8: error: overriding 'A<TType> A<TType>::operator+(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<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
.
User: "Alf P. Steinbach"

Title: Re: invalid covariant return type 06 Dec 2007 06:23:35 PM
* 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: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
.

User: "Alf P. Steinbach"

Title: Re: invalid covariant return type 06 Dec 2007 06:25:15 PM
* 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: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
.



  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER