Re: doubts on operator
alessio211734 wrote:
I have a class that defines a generic point where p={a1..an}
template<class T,int i> class Point
{
public:
T v[i];
Public? Really?
Point<T,i>()
First off, you don't need to repeat "<T,i>" *inside* the template class
definition.
{
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;
So, my guess would be that 'Point2f' is a typedef, something like
typedef Point2<float> Point2f;
Next time, please, be verbose about those things.
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)
[..]
Why Point2 don't call the operator= defined in Point<i,T>?
Because Point2 has its *own* operator=, which *hides* any that it can
inherit from its base class.
Point2 is a Point<i,T> because derive by Point<i,T>, I don't
understand...
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask