Gabriel Fleseriu
October 4th, 2003, 06:33 PM
I could bang my head against the keyboard. Repeatedly. Just spent two hours to debug a piece of code that seemed trivial. Almost begun to suspect "that famous bug of the compiler, that you always seem to discover when your code behaves strange".
Do you see it? It's in the ctor. Unbelivable that I wrote an article about vectors...
class coordinates
{
public:
coordinates():data(3){};
coordinates(double x_, double y_, double z_)
{
data.reserve(3);
data.[0] = x_;
data.[1] = y_;
data.[2] = z_;
}
double& x(){return data[0];}
double& y(){return data[1];}
double& z(){return data[2];}
double x()const{return data[0];}
double y()const{return data[1];}
double z()const{return data[2];}
double& operator[](int i){return data[i];}
double operator[](int i) const{return data[i];}
operator double*(){return &data[0];}
bool operator ==(const coordinates& rhs)const{return (x()==rhs.x()) && (y() == rhs.y()) && (z() == rhs.z());}
bool operator !=(const coordinates& rhs)const{return !(*this==rhs);}
coordinates & operator +=(const coordinates & rhs)
{
x()+=rhs.x();
y()+=rhs.y();
z()+=rhs.z();
return *this;
}
coordinates & operator -=(const coordinates & rhs)
{
x()-=rhs.x();
y()-=rhs.y();
z()-=rhs.z();
return *this;
}
coordinates operator-(){return coordinates(-x(), -y(), -z());}
double length()const{return sqrt(data[0]*data[0] + data[1]*data[1] + data[2]*data[2]);}
void normalize()
{
double l = length();
if(!l){
math_error err;
throw err;
}
else{
data[0]/=l;
data[1]/=l;
data[2]/=l;
}
}
coordinates normalized()const{coordinates temp(*this); temp.normalize(); return temp;}
private:
std::vector<double> data;
};
Do you see it? It's in the ctor. Unbelivable that I wrote an article about vectors...
class coordinates
{
public:
coordinates():data(3){};
coordinates(double x_, double y_, double z_)
{
data.reserve(3);
data.[0] = x_;
data.[1] = y_;
data.[2] = z_;
}
double& x(){return data[0];}
double& y(){return data[1];}
double& z(){return data[2];}
double x()const{return data[0];}
double y()const{return data[1];}
double z()const{return data[2];}
double& operator[](int i){return data[i];}
double operator[](int i) const{return data[i];}
operator double*(){return &data[0];}
bool operator ==(const coordinates& rhs)const{return (x()==rhs.x()) && (y() == rhs.y()) && (z() == rhs.z());}
bool operator !=(const coordinates& rhs)const{return !(*this==rhs);}
coordinates & operator +=(const coordinates & rhs)
{
x()+=rhs.x();
y()+=rhs.y();
z()+=rhs.z();
return *this;
}
coordinates & operator -=(const coordinates & rhs)
{
x()-=rhs.x();
y()-=rhs.y();
z()-=rhs.z();
return *this;
}
coordinates operator-(){return coordinates(-x(), -y(), -z());}
double length()const{return sqrt(data[0]*data[0] + data[1]*data[1] + data[2]*data[2]);}
void normalize()
{
double l = length();
if(!l){
math_error err;
throw err;
}
else{
data[0]/=l;
data[1]/=l;
data[2]/=l;
}
}
coordinates normalized()const{coordinates temp(*this); temp.normalize(); return temp;}
private:
std::vector<double> data;
};