Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/163.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 增强共享的ptr和c++;参考资料。什么';这到底是怎么回事?_C++_Boost_Reference_Shared Ptr - Fatal编程技术网

C++ 增强共享的ptr和c++;参考资料。什么';这到底是怎么回事?

C++ 增强共享的ptr和c++;参考资料。什么';这到底是怎么回事?,c++,boost,reference,shared-ptr,C++,Boost,Reference,Shared Ptr,下面的代码是创建一个B链,由方法f遍历 如下所示,代码不起作用。每次遍历只深入一层 我已经了解到,链应该只返回一个共享的\u ptr,但我的问题是,为什么这不起作用 #include <iostream> #include <boost/shared_ptr.hpp> class B { public: B() { } B(const B& b) { } B& chain() { b = boost::shar

下面的代码是创建一个B链,由方法f遍历

如下所示,代码不起作用。每次遍历只深入一层

我已经了解到,链应该只返回一个共享的\u ptr,但我的问题是,为什么这不起作用

#include <iostream>
#include <boost/shared_ptr.hpp>

class B
{
public:
  B()
  {
  }

  B(const B& b)
  {
  }

  B& chain()
  {
    b = boost::shared_ptr<B>(new B());

    return *b;
  }

  void f()
  {
    std::cout << this << " " << bool(b) << std::endl;

    if (b)
      return b->f();

    return;
  }

  boost::shared_ptr<B> b;
};

int main()
{
  B b0;
  B b1 = b0.chain();
  B b2 = b1.chain();

  b0.f();
  b1.f();
  b2.f();
}
#包括
#包括
B类
{
公众:
B()
{
}
B(康斯特食宿酒店)
{
}
B&chain()
{
b=boost::shared_ptr(新的b());
返回*b;
}
void f()
{

std::cout因为当您将其分配给非参考变量
b1
b2
时,会进行复制。而且由于您有一个不执行任何操作的复制构造函数,因此不会复制成员变量

要么删除复制构造函数,要么正确实现它