Boost shared_ptr and c++ references. What's wrong here, exactly? -
the code below create chain of b's, traversed method f.
as presented below, code doesn't work. each traversal goes 1 level deep.
i've learned chain should return shared_ptr, question why doesn't work?
#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 << << " " << 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(); }      
because when assign non-reference variables b1 , b2, copies made. , since have copy-constructor nothing, member variable not copied.
either remove copy-constructor, or implement properly.
Comments
Post a Comment