假设我们有一个抽象类Element
,其中类Triangle
和Quadrilateral
派生自。
假设这些类与取决于元素形状的插值方法结合使用。所以,基本上我们创建了一个抽象类InterpolationElement
,我们从中派生InterpolationTriangle
和InterpolationQuadrilateral
。
Triangle
然后,为了在和Quadrilateral
类中包含插值功能,我们在Element
type 类中添加了一个 const-reference 数据成员InterpolationElement
,即:
class Element
{
public:
Element(const InterpolationElement& interp);
const InterpolationElement& getInterpolation() const;
private:
const InterpolationElement& interpolation;
};
然后,我们创建一个方法(如 Scott Meyers,Effective C++ 所述),将类的本地静态对象实例InterpolationTriangle
化为
const InterpolationTriangle& getInterpolationTriangle()
{
static InterpolationTriangle interpolationTriangle;
return interpolationTriangle;
}
所以这个类Triangle
可以像这样构造:
class Triangle : public Element
{
public:
Triangle() : Element( getInterpolationTriangle() ) {}
};
这是我的问题:为了在我的班级中加入插值方法,这种方法是否正确Element
?这是在专业场景中使用的吗?
我可以直接在类(作为纯虚拟)上实现所有插值方法,Element
并在派生类Triangle
和Quadrilateral
. 然而,这种方法在我看来很麻烦,因为每次我需要改进或实现新的插值功能时,我都必须在这些类上这样做。此外,使用这种方法,类变得越来越大(许多方法)。
我想听听你的一些建议和意见
提前致谢。
额外细节:
class InterpolationElement
{
public:
InterpolationElement();
virtual double interpolationMethod1(...) = 0;
:
virtual double interpolationMethodN(...) = 0;
}
class InterpolationTriangle : public InterpolationElement
{
public:
InterpolationTriangle () {}
virtual double interpolationMethod1(...) { // interpolation for triangle }
:
virtual double interpolationMethodN(...) { // interpolation for triangle }
}
class InterpolationQuadrilateral : public InterpolationElement
{
public:
InterpolationTriangle () {}
virtual double interpolationMethod1(...) { // interpolation for quadrilateral}
:
virtual double interpolationMethod1(...) { // interpolation for quadrilateral}
}