本文介绍了C ++:作为成员的抽象类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我对风格有疑问.我有一个类(在我的情况下为Option),该类取决于外生对象的值(利率).我的目标是为外部对象(Rate)创建一个抽象基类,以便我可以构造变体(例如SimulatedRate或ConstantRate),这些变体将在我的依赖类Option中工作.

I have a question about style. I have a class (in my case an Option) that depends on the value of an exogenous object (Interest Rate). My goal is to create a abstract base class for the exogenous object (Rate) so that I can construct variations, say SimulatedRate or ConstantRate, that will work inside my depending class, Option.

但是,我在C ++中发现,因为显然不能实例化抽象基类,所以必须存储指向基类的指针或引用.我担心的是,当实例化的外源对象超出依赖类之外的范围时,我的依赖类将指向垃圾.

However, I'm finding in C++, since I obviously cannot instantiate a abstract base class, I must store either a pointer or a reference to the base class. My concern is that when the instantiated exogenous objects go out of scope outside of the dependent class, my dependent class will be pointing to junk.

在C ++中是否存在合理的方法来利用多态性解决此问题?

Is there a reasonable way to utilize polymorphism for this problem in C++?

我当前的代码:

class Dependent
{
public:
    Dependent(const Exogenous& exo) : exo_(exo) {}
    double getSomething() const { exo_.interfaceMethod(); }
private:
    Exogenous& exo_;
}

class Exogenous
{
public:
    virtual double interfaceMethod() const=0;
}

class ExogenousVariationA
{
public:
    virtual double interfaceMethod() const { return resultA; }
}

class ExogenousVariationB
{
public:
    virtual double interfaceMethod() const { return resultB; }
}

推荐答案

sftrabbit有一些很好的建议,我要补充一下:

sftrabbit has some good advice, to which I'd add:

  • 您可以在抽象基类中创建virtual clone()方法(它不是 virtual 基类-完全是其他东西);该方法将在派生的利率类中实现,返回指向期权可以拥有的新的独立利率对象的指针;如果对象包含的数据在您使用时会发生变化(例如从计算或缓存中更改),这将特别有用
  • 您可能不希望这样,但是使用std/boost共享指针,也可以要求指向共享对象的弱指针...这样,您可以测试对象的所有者"( '不包括您)已经完成并触发了它的破坏
  • you could create a virtual clone() method in the abstract base class (it's not a virtual base class - that's something else entirely); that method would be implemented in the derived interest rate classes, returning a pointer to a new independent interest rate object that can be owned by the Option; this is particularly useful if the objects contain data that changes as you use it (e.g. from calculations or caching)
  • you probably don't want this, but with std/boost shared pointers it's also possible to ask for a weak pointer to the shared object... that way you can test whether the "owners" of the object (which won't include you) have already finished with it and triggered its destruction

另外,要使用运行时多态性,您的ExogenousVariationA和〜B类必须实际上是从Exogenous派生的,并且要进行多态调度的方法必须是virtual.看起来像这样:

Separately, to use runtime polymorphism your ExogenousVariationA and ~B classes must actually derive from Exogenous, and the method you want to be polymorphically dispatched must be virtual. That looks like this:

class Exogenous
{
public:
    virtual double interfaceMethod() const=0;
}

class ExogenousVariationA : public Exogenous
{
public:
    double interfaceMethod() const { return resultA; }
}

这篇关于C ++:作为成员的抽象类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 09:40