问题描述
我创建了一个类来表示向量 (Vec3D),以及它的基本规范运算(求和、标量积、叉积等)。我将使用这个类来推导真实物理单位的类(位置、速度、加速度、力等),这些类将具有自己的“专有”特性和操作,*除了它们的矢量操作 .
我希望 Vec3D 是不可实例化的,因为一切都必须是一个物理单位,但使其抽象意味着我必须再次为每个派生类重新定义所有运算符。
这里的最佳实践是什么,它允许我利用已经定义的运算符? (也许将构造函数定义为私有,将运算符定义为公共?
和
在什么情况下我需要使用虚函数?
在我的基类下面:
class Vec3D
{
private:
double _x;
double _y;
double _z;
protected:
// Constructors / Destructor
Vec3D(const double x,const double y,const double z);
Vec3D();
Vec3D(const Vec3D& source);
Vec3D(Vec3D&& source);
~Vec3D(); // Declared just for completeness,although not necessary
public:
// Operator Overloading
Vec3D& operator=(const Vec3D& source);
Vec3D& operator=(Vec3D&& source);
Vec3D& operator+=(const Vec3D& source);
Vec3D& operator+=(Vec3D&& source);
Vec3D operator+(const Vec3D& source) const;
Vec3D operator+(Vec3D&& source) const;
Vec3D operator-() const;
Vec3D& operator-=(const Vec3D& source);
Vec3D& operator-=(Vec3D&& source);
Vec3D operator-(const Vec3D& source)const;
Vec3D operator-(Vec3D&& source) const;
Vec3D operator*(const double source) const;
double operator*(const Vec3D& source) const;
double operator*(Vec3D&& source) const;
Vec3D operator^(const Vec3D& source) const;
Vec3D operator^(Vec3D&& source) const;
};
谢谢
解决方法
为了解决您的问题,您可以做一些类似于我最近为工作所做的事情。
我使用了继承的组合,使用 Curiously Recurring Template Pattern (CRTP) 作为 mixin,然后是赋值运算符的宏。
对于您的情况,您的赋值运算符可以使用普通的受保护成员函数将实际实现卸载到基类。
也许是这样的:
template<typename RealVec>
class VecBase
{
public:
friend RealVec operator+(RealVec lhs,RealVec const& rhs)
{
return lhs += rhs;
}
friend RealVec operator*(RealVec lhs,RealVec const& rhs)
{
return lhs *= rhs;
}
protected:
// Compound addition and assignment
RealVec& add_assign_this(RealVec& lhs,RealVec const& rhs)
{
// To prevent self-assignment
if (&lhs != &rhs)
{
lhs._x += rhs._x;
lhs._y += rhs._y;
lhs._z += rhs._z;
}
return lhs;
}
// Compound multiplication and assignment
RealVec& mul_assign_this(RealVec& lhs,RealVec const& rhs)
{
// To prevent self-assignment
if (&lhs != &rhs)
{
lhs._x *= rhs._x;
lhs._y *= rhs._y;
lhs._z *= rhs._z;
}
return lhs;
}
// Plain assignment,=
RealVec& assign_this(RealVec& lhs,RealVec const& rhs)
{
// To prevent self-assignment
if (&lhs != &rhs)
{
static_cast<VecBase&>(lhs) = rhs;
}
return lhs;
}
// Protected constructors to inhibit instantiation
VecBase()
: _x{ 0 },_y{ 0 },_z{ 0 }
{
}
VecBase(int x,int y,int z)
: _x{ x },_y{ y },_z{ z }
{
}
VecBase(VecBase const&) = default;
VecBase(VecBase&&) = default;
private:
int _x;
int _y;
int _z;
// The default assignment operator will do the correct thing,no need to implement it
VecBase& operator=(VecBase const&) = default;
};
class Vector3D : public VecBase<Vector3D>
{
public:
// Unfortunately we need to reimplement the constructors,// but can "call" the base constructors
Vector3D()
: VecBase<Vector3D>{ }
{
}
Vector3D(int x,int z)
: VecBase<Vector3D>{ x,y,z }
{
}
Vector3D(Vector3D const& other)
: VecBase<Vector3D>(other)
{
}
#define DEFINE_COMPOUND_ASSIGNMENT_OPERATOR(oper,func) \
Vector3D& operator oper (Vector3D const& rhs) \
{ \
return VecBase<Vector3D>::func##_assign_this(*this,rhs); \
}
DEFINE_COMPOUND_ASSIGNMENT_OPERATOR(+=,add)
DEFINE_COMPOUND_ASSIGNMENT_OPERATOR(*=,mul)
#undef DEFINE_COMPOUND_ASSIGNMENT_OPERATOR
// We still need to explicitly define the plain assignment operator
Vector3D& operator=(Vector3D const& rhs)
{
return VecBase<Vector3D>::assign_this(*this,rhs);
}
};
[See it running on the Compiler Explorer site]
在继承的类中还有很多要写,遗憾的是真的无法避免。