doubts on operator

A

alessio211734

I have a class that defines a generic point where p={a1..an}

template<class T,int i> class Point
{
public:
T v;

Point<T,i>()
{
for (int j=0;j<i;j++) v[j]=0;
}


inline T & operator [] ( const int j ){
assert(j>=0 && j<i);
return v[j];
}
inline const T & operator [] ( const int j ) const {
assert(j>=0 && j<i);
return v[j];
}


inline Point<T,i>& operator=(const Point<T,i> & num)
{
for (int j=0;j<i;j++) v[j]=num.v[j];
return *this;

}


inline Point<T,i>& operator=(const float* pnum)
{
for (int j=0;j<i;j++) v[j]=pnum[j];
return *this;

}


inline Point<T,i> operator + ( Point<T,i> const & num) const {
Point<T,i> vect;
for (int j=0;j<i;j++) vect.v[j]=v[j]+num.v[j];
return vect;
}
inline Point<T,i> operator - ( Point<T,i> const & num) const {
Point<T,i> vect;
for (int j=0;j<i;j++) vect.v[j]=v[j]-num.v[j];
return vect;
}
inline Point<T,i> operator * ( const T s ) const {
Point<T,i> vect;
for (int j=0;j<i;j++) vect.v[j]=v[j]*s;
return vect;
}

inline Point<T,i>& operator *= ( const T s ){
for (int j=0;j<i;j++) v[j]=v[j]*s;
return *this;
}
}


I define a new class Point2

template<class T> class Point2: public Point<T,2>
{
public:
Point2<T>()
{
v[0]=0;
v[1]=1;
}

Point2<T>(const T& a, const T& b)
{
v[0]=a;
v[1]=b;
}

T x() { return v[0] };
T y() { return v[1] };

};

and in main program I run:

Point2f a;
Point2f b;
Point2f c;
c=a+b;

I get this error:

error C2679: binary '=' : no operator found which takes a right-hand
operand of type 'Point<T,i>' (or there is no acceptable conversion)
with
[
T=float,
i=2
]
could be 'Point2<T> &Point2<T>::eek:perator =(const Point2<T>
&)'
with
[
T=float
]
while trying to match the argument list '(Point2f,
Point<T,i>)'
with
[
T=float,
i=2
]


Why Point2 don't call the operator= defined in Point<i,T>?

Point2 is a Point<i,T> because derive by Point<i,T>, I don't
understand...


Please explain me.
 
A

alessio211734

Because Point2 has its *own* operator=, which *hides* any that it can
inherit from its base class.

What's the operator= that Point2 define?
Point2 define a default operator?
What's the advantages of hereditance if I should re-define an operator
for Point2 again?
Can't use operator= and other operators defined in the base class
without re-define in the derived class Point2?
 
M

Markus Moll

Hi
What's the operator= that Point2 define?
Point2 define a default operator?
What's the advantages of hereditance if I should re-define an operator
for Point2 again?
Can't use operator= and other operators defined in the base class
without re-define in the derived class Point2?

An assignment operator for a base class is rarely useful. Consider:
A a;
B b;
a = b;

Without additional knowledge (i.e. without a user-defined assignment
operator), this only makes sense if b contains all the parts of an A,
which is the case if B is equal to or derived from A. If A is derived from
B, on the other hand, then how would you fill in the missing data?

Markus
 
A

alessio211734

Hi



An assignment operator for a base class is rarely useful. Consider:
A a;
B b;
a = b;

Without additional knowledge (i.e. without a user-defined assignment
operator), this only makes sense if b contains all the parts of an A,
which is the case if B is equal to or derived from A. If A is derived from
B, on the other hand, then how would you fill in the missing data?

Markus


I would like define constructors for n-dimensional point.
If I include in the base class these constructors my application work,
but I think that this is not safe because
I can call the constructor with three parameters when I use
Point<float,2> for example.


Point<T,i>(const T& a, const T& b)
{
assert(i==2);
v[0]=a;
v[1]=b;
}

Point<T,i>(const T& a, const T& b,const T& c)
{
assert(i==3);
v[0]=a;
v[1]=b;
v[2]=c;
}

I think to delete constructors in base classes and extends the base
class with
for example Point2 derived by Point and in this class define the
constructor with 2 parameters.

What's syntax should use in Point2 for utilize Point operators?
 
I

Ian Collins

alessio211734 said:
I have a class that defines a generic point where p={a1..an}

template<class T,int i> class Point
{
public:
T v;

Point<T,i>()
{
for (int j=0;j<i;j++) v[j]=0;
}


inline T & operator [] ( const int j ){


In addition to Victor's comments, the 'inline' is superfluous here. All
member functions defined in the class declaration are inline.
 

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,772
Messages
2,569,593
Members
45,112
Latest member
VinayKumar Nevatia
Top