我有一個類:奇怪的行爲
class Point3D : public Point{
protected:
float x;
float y;
float z;
public:
Point3D(){x=0; y=0; z=0;}
Point3D(const Point3D & point){x = point.x; y = point.y; z = point.z;}
Point3D(float _x,float _y,float _z){x = _x; y = _y; z = _z;}
inline const Point3D operator+(const Vector3D &);
const Point3D & operator+(const Point3D &point){
float xT = x + point.getX();
float yT = y + point.getY();
float zT = z + point.getZ();
return Point3D(xT, yT, zT);
}
...
當我使用這種方式:
Point3D point = Point3D(10,0,10);
一切正常。
當我寫:
Point3D point = Point3D(10,0,10);
Point3D point2 = Point3D(0,0,0) + point();
而且它的確定(點2 =點)。當我添加超過(0,0,0)的東西時,它也在工作。
但是,當我想剛:
Point3D point = Point3D(10,0,10);
someFunction(Point3D(0,0,0) + point); //will get strange (x,y,z)
的一些函數來獲取值(在我看來)隨機的(X,Y,Z)。爲什麼?
什麼是更奇怪的,因爲類似的例子一切將再次合作:
Point3D point = Point3D(10,0,10);
Point3D point2 = Point3D(0,0,0) + point;
someFunction(point2); // will get (10,0,10)
什麼是該異常行爲的原因是什麼?
在這篇文章請看:http://stackoverflow.com/questions/4421706/operator-overloading –