【问题标题】:Initialization of "nested" boost::shared_ptr class members“嵌套” boost::shared_ptr 类成员的初始化
【发布时间】:2016-03-10 01:46:52
【问题描述】:

我正在尝试编写一个具有 3 个抽象类(Base_ABase_BBase_C)的 C++ 程序,并且它们每个都有一个派生类(分别为 Derived_ADerived_B 和 @987654326 @)。

现在,我想创建Derived_C 的对象z(通过boost::shared_ptr)。要初始化z 我想使用y(通过boost::shared_ptr),它是Derived_B 的成员,并初始化y 我想使用x(通过boost::shared_ptr),它反过来又是Derived_A 的成员。

我面临的问题是我想检索成员K的值,它属于类Derived_B,但是下面这行

std::cout << z->barC->K << std::endl;

返回 0.0 而不是 50.0(应该是这样)。

我错过了什么?可能与 Derived_BDerived_C 类的构造函数中的 shared_ptr 成员的初始化有关?

代码如下:

//----- 啊.h -----

class Base_A{  
public:  
  double K;  

  Base_A(){};
  Base_A(const double& _K);
  virtual ~Base_A(){};

  virtual double operator()(const double& _S) const = 0;
};

class Derived_A :public Base_A{
public:
  double K;

  Derived_A(const double& _K);
  ~Derived_A(){};

  double operator()(const double& _S)const;
};

//-- A.cpp -----

Base_A::Base_A(const double& _K)
  :K(_K){};

Derived_A::Derived_A(const double& _K)
  :K(_K){};

double Derived_A::operator()(const double& _S) const {
  return std::max(_S - K, 0.0);
}

//--------------------------------------------- ----------------------------------

//----- B.h -----

class Base_B{
public:
  double S;
  double K;
  boost::shared_ptr<Base_A> fooB;

  Base_B(){};
  virtual ~Base_B(){};

  // other pure virtual functions...
};

class Derived_B :public Base_B{
public:
   double S;
   double K;
   boost::shared_ptr<Derived_A> fooD;

   Derived_B(const double& _K, boost::shared_ptr<Derived_A> _fooD);
   ~Derived_B(){};

   // declaration of the pure virtual functions...
};

//----- B.cpp -----

Derived_B::Derived_B(const double& _K, boost::shared_ptr<Derived_A> _fooD)
  :K(_K){
  fooD = boost::shared_ptr<Derived_A>(_fooD);
};

//--------------------------------------------- --------------------------------

//----- C.h -----

class Base_C{
public:
  boost::shared_ptr<Base_B> barB;

  Base_C(){};
  virtual ~Base_C(){};

  // other pure virtual functions...
};

class Derived_C :public Base_C{
public:
  boost::shared_ptr<Base_B> barC;

  Derived_C(boost::shared_ptr<Base_B> _barC);
  ~Derived_C(){};
  // declaration of the pure virtual functions...
};

//-- C.cpp -----

Derived_C::Derived_C(boost::shared_ptr<Base_B> _barC){

  barC = _barC;
};

//--------------------------------------------- ----------------------------------

//----- Main.cpp -----

void main(){

  double K= 50.0;
  double S= 100.0;

  boost::shared_ptr<Derived_A> x(new Derived_A(K));

  boost::shared_ptr<Derived_B> y(new Derived_B(K,x));

  boost::shared_ptr<Derived_C> z(new Derived_C(y));

  std::cout << z->barC->K << std::endl; // returns 0.0 instead of 50.0
}

提前致谢。

【问题讨论】:

    标签: c++ boost polymorphism shared-ptr


    【解决方案1】:

    我没有测试过这个答案。

    您似乎在每个班级都重新声明double K;。每个新声明都会屏蔽父类中的K

    您不是从Base_A 读取K,您可能是从Derived_B 获得的,但指针z-&gt;barC 指向Base_B

    【讨论】:

    • 感谢@Zan Lynx,我已经更改了Derived_B 的构造函数,现在它看起来像Derived_B( boost::shared_ptr&lt;Derived_A&gt; _fooD){ barC = _barC; };。所以在main 中,我创建了一个Derived_B member 作为boost::shared_ptr&lt;Derived_B&gt; y(new Derived_B(x));。现在:std::cout &lt;&lt; y-&gt;fooD-&gt;K &lt;&lt; std::endl; 正确返回 50.0,std::cout &lt;&lt; z-&gt;barC-&gt;K &lt;&lt; std::endl; 返回 0 而不是 50.0,std::cout &lt;&lt; z-&gt;barC-&gt;fooB-&gt;K&lt;&lt; std::endl; 给出 Assertion failed: px != 0, file d:\boost_1_57_0\boost\smart_ptr\shared_ptr.hpp, line 648。因此,当我通过x 初始化y 时,我没有通过K
    猜你喜欢
    • 1970-01-01
    • 2011-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-19
    • 1970-01-01
    相关资源
    最近更新 更多