covariant template problem

W

weinma81

Hi!

I'm writing a vector-class for some calculations and it look's like i
am having a covariant problem, where i shouldn't:

The Vector-Class Look's like this and works:

template <int _size,class T>
class Vector {
protected:
T vector[_size];
public:
...
virtual Vector<_size,T> operator-(const Vector<_size,T> &v)
const; // that's one of the methods causing problems

As the crossproduct is only defined for a 3D Vector i wanted to write
and inherit a Vector3 from Vector.
The operators should also return the Vector3 so i overwrote them.. and
there comes my covariant problem.
Looks like this:

template <class T>
class Vector3 : public Vector<3,T> {
public:
...
Vector3<T> operator-(const Vector<3,T> &v) const; // <-- Theres my
problem

Compiler cries:
.../Vector3.h:56: error: invalid covariant return type for 'Vector3<T>
Vector3<T>::eek:perator-(const Vector<3, T>&) const [with T = float]'
.../Vector.h:72: error: overriding 'Vector<_size, T> Vector<_size,
T>::eek:perator-(const Vector<_size, T>&) const [with int _size = 3, T =
float]'

don't know what to do.. asked some people but they also said that this
should work..

regards
Weinma
 
K

Kai-Uwe Bux

Hi!

I'm writing a vector-class for some calculations and it look's like i
am having a covariant problem, where i shouldn't:

The Vector-Class Look's like this and works:

template <int _size,class T>
class Vector {
protected:
T vector[_size];
public:
...
virtual Vector<_size,T> operator-(const Vector<_size,T> &v)
const; // that's one of the methods causing problems

Why is that method virtual? Virtual binary operators do not really work all
that well.

As the crossproduct is only defined for a 3D Vector i wanted to write
and inherit a Vector3 from Vector.

Don't. Just have a free standing function:

template < typename T >
Vector<3,T>
cross_product ( Vector<3,T> const & lhs, Vector<3,T> const & rhs ) {
...
}

Note that with the inheritance approach, you create two types: Vector3<T>
The operators should also return the Vector3 so i overwrote them.. and
there comes my covariant problem.
Looks like this:

template <class T>
class Vector3 : public Vector<3,T> {
public:
...
Vector3<T> operator-(const Vector<3,T> &v) const; // <-- Theres my
problem

Compiler cries:
../Vector3.h:56: error: invalid covariant return type for 'Vector3<T>
Vector3<T>::eek:perator-(const Vector<3, T>&) const [with T = float]'
../Vector.h:72: error: overriding 'Vector<_size, T> Vector<_size,
T>::eek:perator-(const Vector<_size, T>&) const [with int _size = 3, T =
float]'

don't know what to do.. asked some people but they also said that this
should work..

Covariant return types are restricted to pointers and references to classes.
See [10.3/5] for details.


Best

Kai-Uwe Bux
 
M

Michael DOUBEZ

(e-mail address removed) a écrit :
Hi!

I'm writing a vector-class for some calculations and it look's like i
am having a covariant problem, where i shouldn't:

The Vector-Class Look's like this and works:

template <int _size,class T>
class Vector {
protected:
T vector[_size];
public:
...
virtual Vector<_size,T> operator-(const Vector<_size,T> &v)
const; // that's one of the methods causing problems

As the crossproduct is only defined for a 3D Vector i wanted to write
and inherit a Vector3 from Vector.
The operators should also return the Vector3 so i overwrote them.. and
there comes my covariant problem.
Looks like this:

template <class T>
class Vector3 : public Vector<3,T> {
public:
...
Vector3<T> operator-(const Vector<3,T> &v) const; // <-- Theres my
problem

Compiler cries:
../Vector3.h:56: error: invalid covariant return type for 'Vector3<T>
Vector3<T>::eek:perator-(const Vector<3, T>&) const [with T = float]'
../Vector.h:72: error: overriding 'Vector<_size, T> Vector<_size,
T>::eek:perator-(const Vector<_size, T>&) const [with int _size = 3, T =
float]'

don't know what to do.. asked some people but they also said that this
should work..

Covariance works only with pointers and references.

Here you return by value.

The idiom here would be to rather define:
template <int _size,class T>
virtual Vector<_size,T>& Vector<_size,T>::eek:perator-=(const
Vector<_size,T> &v)
{
//substract
return *this;
}

and defines the operations

template <int _size,class T>
Vector<_size,T> operator-(const Vector<_size,T> &lhs,
const Vector<_size,T> &rhs)
{
Vector<_size,T> ret(lhs);
ret-=rhs;
return ret;
}

Then you only need
template <class T>
virtual Vector3<T>& Vector3<T>::eek:perator-=(const Vector<3,T> &v)
{
//special 3 dim op
return *this;
}



Michael
 
M

Michael DOUBEZ

Michael DOUBEZ a écrit :
(e-mail address removed) a écrit :
Hi!

I'm writing a vector-class for some calculations and it look's like i
am having a covariant problem, where i shouldn't:

The Vector-Class Look's like this and works:

template <int _size,class T>
class Vector {
protected:
T vector[_size];
public:
...
virtual Vector<_size,T> operator-(const Vector<_size,T> &v)
const; // that's one of the methods causing problems

As the crossproduct is only defined for a 3D Vector i wanted to write
and inherit a Vector3 from Vector.
The operators should also return the Vector3 so i overwrote them.. and
there comes my covariant problem.
Looks like this:

template <class T>
class Vector3 : public Vector<3,T> {
public:
...
Vector3<T> operator-(const Vector<3,T> &v) const; // <-- Theres my
problem

Compiler cries:
../Vector3.h:56: error: invalid covariant return type for 'Vector3<T>
Vector3<T>::eek:perator-(const Vector<3, T>&) const [with T = float]'
../Vector.h:72: error: overriding 'Vector<_size, T> Vector<_size,
T>::eek:perator-(const Vector<_size, T>&) const [with int _size = 3, T =
float]'

don't know what to do.. asked some people but they also said that this
should work..

Covariance works only with pointers and references.

Here you return by value.

The idiom here would be to rather define:
template <int _size,class T>
Vector<_size,T>& Vector<_size,T>::eek:perator-=(const
Vector<_size,T> &v)
{
//substract
return *this;
}

and defines the operations

template <int _size,class T>
Vector<_size,T> operator-(const Vector<_size,T> &lhs,
const Vector<_size,T> &rhs)
{
Vector<_size,T> ret(lhs);
ret-=rhs;
return ret;
}

Then you only need
template <class T>
Vector3<T>& Vector3<T>::eek:perator-=(const Vector<3,T> &v)
{
//special 3 dim op
return *this;
}

and of course

template <class T>
Vector3<T> operator-(const Vector<3,T> &lhs,
const Vector<3,T> &rhs)
{
Vector3<T> ret(lhs);
lhs-=rhs;
return ret;
}

Michael
 

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,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top