C ++避免代码重复运算符重载
c++ avoid code duplication operator overloading
我的问题如下,假设我有这个 classes:
class ID3OBJ
{
public:
const double X;
const double Y;
const double Z;
ID3OBJ(double x, double y, double z);
ID3OBJ();
const ID3OBJ operator+ (const ID3OBJ param) const;
}
class Vector : public ID3OBJ
{
public:
Vector(double x, double y, double z);
Vector();
/*const Vector const operator+ (const Vector param); */
}
class Point : public ID3OBJ
{
public:
Point(double x, double y, double z);
Point();
}
class Plane : public ID3OBJ
{
public:
Point Origin;
Vector Direction1;
Vector Direction2;
Vector NormalVector;
Plane(Point Origin, Vector Normalvector);
Plane(Point Origin, Vector Direction1, Vector Direction2);
Plane();
}
class D3GON : public ID3OBJ, public Plane
{
public:
std::vector<Point> EdgePoints;
D3GON(Point P1, Point P2, Point P3);
};
在我当前的代码中,我必须为每个 class 重新定义运算符重载,我怎样才能避免这种代码重复?
我必须添加转换函数吗?
我使用 const member-values 来拒绝对象创建后的更改。这意味着如果必须更改任何低级对象,则必须将其替换为新对象。请参阅下面我的运算符重载:
// operator overwrites:
const ID3OBJ ID3OBJ::operator+ (const ID3OBJ param) const { double newX, newY, newZ; newX = X + param.X; newY = Y + param.Y; newZ = Z + param.Z; return ID3OBJ(newX, newY, newZ); }
谢谢:)
Curiously recurring template pattern 是去这里的路。它稍微复杂一些,因为您需要多个派生级别。但这里有一个代码示例:
template <class T>
class ID3OBJ
{
public:
double X,Y,Z;
T operator+(const T& obj) const {
T t;
t.X = X + obj.X;
t.Y=Y+obj.Y;
t.Z=z+obj.Z;
return t;
}
};
class Vector : public ID3OBJ<Vector>
{
public:
// some stuff
};
class Point : public Vector, public ID3OBJ<Point>
{
public:
// X, Y and Z exist twice, once in Vector, once in ID3OBJ<Point>, so we must disambiguate
using ID3OBJ<Point>::X;
using ID3OBJ<Point>::Y;
using ID3OBJ<Point>::Z;
};
您可以添加 Vector
(您将得到 Vector
)或仅添加 Point
,您将得到 Point
。更有趣的是,如果您添加 Point
和 Vector
,您会得到 Vector
结果,因为重载运算符 + 解析将 select Vector::operator +
我的问题如下,假设我有这个 classes:
class ID3OBJ
{
public:
const double X;
const double Y;
const double Z;
ID3OBJ(double x, double y, double z);
ID3OBJ();
const ID3OBJ operator+ (const ID3OBJ param) const;
}
class Vector : public ID3OBJ
{
public:
Vector(double x, double y, double z);
Vector();
/*const Vector const operator+ (const Vector param); */
}
class Point : public ID3OBJ
{
public:
Point(double x, double y, double z);
Point();
}
class Plane : public ID3OBJ
{
public:
Point Origin;
Vector Direction1;
Vector Direction2;
Vector NormalVector;
Plane(Point Origin, Vector Normalvector);
Plane(Point Origin, Vector Direction1, Vector Direction2);
Plane();
}
class D3GON : public ID3OBJ, public Plane
{
public:
std::vector<Point> EdgePoints;
D3GON(Point P1, Point P2, Point P3);
};
在我当前的代码中,我必须为每个 class 重新定义运算符重载,我怎样才能避免这种代码重复?
我必须添加转换函数吗?
我使用 const member-values 来拒绝对象创建后的更改。这意味着如果必须更改任何低级对象,则必须将其替换为新对象。请参阅下面我的运算符重载:
// operator overwrites:
const ID3OBJ ID3OBJ::operator+ (const ID3OBJ param) const { double newX, newY, newZ; newX = X + param.X; newY = Y + param.Y; newZ = Z + param.Z; return ID3OBJ(newX, newY, newZ); }
谢谢:)
Curiously recurring template pattern 是去这里的路。它稍微复杂一些,因为您需要多个派生级别。但这里有一个代码示例:
template <class T>
class ID3OBJ
{
public:
double X,Y,Z;
T operator+(const T& obj) const {
T t;
t.X = X + obj.X;
t.Y=Y+obj.Y;
t.Z=z+obj.Z;
return t;
}
};
class Vector : public ID3OBJ<Vector>
{
public:
// some stuff
};
class Point : public Vector, public ID3OBJ<Point>
{
public:
// X, Y and Z exist twice, once in Vector, once in ID3OBJ<Point>, so we must disambiguate
using ID3OBJ<Point>::X;
using ID3OBJ<Point>::Y;
using ID3OBJ<Point>::Z;
};
您可以添加 Vector
(您将得到 Vector
)或仅添加 Point
,您将得到 Point
。更有趣的是,如果您添加 Point
和 Vector
,您会得到 Vector
结果,因为重载运算符 + 解析将 select Vector::operator +