Smart pointers - passing 'this' around

Discussion in 'C++' started by Bonzo, Jul 23, 2003.

  1. Bonzo

    Bonzo Guest

    Another smart pointer problem. I have a tree structure. The idea is for
    parents to have SmartPtr's to the children, with children holding a weak
    pointer (WeakPtr) back to the parent. That's great, but how do I
    implement the link from child to parent?

    Here's where the problem occurs:

    SmartPtr<ContainerNode>
    ContainerNode :: CreateChildContainer()
    {
    SmartPtr<ContainerNode> child( new ContainerNode );

    // a) This would cause 'this' to be deallocated at end of scope!
    // SmartPtr<ContainerNode> smart( this );
    // child->SetParent( smart );

    // b) Can't assign raw ptr to WeakPtr in SetParent.
    // child->SetParent( this );

    return child;
    }

    So how do I pass 'this' to the child? A hack to bump up the strong ref
    count in case a) so SmartPtr won't deallocate 'this'? Ugh.


    Instead, the calling code could of course say:

    SmartPtr<ContainerNode> parent;
    SmartPtr<ContainerCode> child;
    ....
    child = parent->CreateChildContainer();
    child->SetParent( parent );

    and it would work fine. The WeakPtr can be assigned to from the
    SmartPtr. But the caller shouldn't have to do that.

    Or am I wrong in assuming I shouldn't be putting raw pointers into weak
    pointers? WeakPtr::getPtr() would return NULL since there'd never be a
    strong ref to the ptr.

    This seems like a ridiculous thing to be stumbling on, but I'm not sure
    what to do. Thanks for any help.

    -jim
    Bonzo, Jul 23, 2003
    #1
    1. Advertising

  2. Bonzo

    Cy Edmunds Guest

    "Bonzo" <> wrote in message
    news:...
    > Another smart pointer problem. I have a tree structure. The idea is for
    > parents to have SmartPtr's to the children, with children holding a weak
    > pointer (WeakPtr) back to the parent. That's great, but how do I
    > implement the link from child to parent?


    Why not just use a raw pointer? Smart pointers are generally used for memory
    management, but I don't suppose you want the children managing memory for
    the parents. (Although sometimes it works that way in real life. heh.)

    >
    > Here's where the problem occurs:
    >
    > SmartPtr<ContainerNode>
    > ContainerNode :: CreateChildContainer()
    > {
    > SmartPtr<ContainerNode> child( new ContainerNode );
    >
    > // a) This would cause 'this' to be deallocated at end of scope!
    > // SmartPtr<ContainerNode> smart( this );
    > // child->SetParent( smart );
    >
    > // b) Can't assign raw ptr to WeakPtr in SetParent.
    > // child->SetParent( this );
    >
    > return child;
    > }


    I don't understand the code fragment above but a typical implementation
    would be like:

    class Child
    {
    private:
    Parent *m_pparent;
    public:
    Child(Parent (i_pparent) : m_pparent(i_pparent) {}
    ...
    };

    class Parent
    {
    public:
    typedef boost::shared_ptr<Child> ChildPtr;
    ChildPtr spawn() {return ChildPtr(new Child(this));
    ...
    };

    <snip>

    --
    Cy
    http://home.rochester.rr.com/cyhome/
    Cy Edmunds, Jul 23, 2003
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. LeTubs
    Replies:
    2
    Views:
    352
    LeTubs
    May 2, 2004
  2. Piet
    Replies:
    0
    Views:
    495
  3. MotoK
    Replies:
    59
    Views:
    1,770
    Keith Thompson
    Sep 15, 2006
  4. n2xssvv g02gfr12930

    Smart pointers and member function pointers

    n2xssvv g02gfr12930, Nov 26, 2005, in forum: C++
    Replies:
    3
    Views:
    454
    n2xssvv g02gfr12930
    Nov 27, 2005
  5. cerr

    pointers, pointers, pointers...

    cerr, Apr 7, 2011, in forum: C Programming
    Replies:
    12
    Views:
    642
Loading...

Share This Page