boost::weak_ptr and shared_ptr pointers from "this"

Discussion in 'C++' started by =?UTF-8?B?UmFmYcWCIE1haiBSYWYyNTY=?=, Apr 5, 2006.

  1. How can I manually (other then
    http://www.boost.org/libs/smart_ptr/enable_shared_from_this.html)
    create a shared, and a weak pointer to "this"?

    Of course I dont want the shared / weak pointer to cause object to be
    deleted when shared/weak ptr will go out of scope.

    --
    Wymiana starych układów... na nowe układy - prawie jak walka z korupcja.
    Walka z wychowaniem seksualnym i erotykÄ… - prawie jak walka z patologiÄ….
    PiS - prawie jak prawo i sprawiedliwość... Prawie. Prawie robi różnicę.
    Myśl. Głosuj rozsądnie. Nie na tanie hasła. // Rafał Maj Raf256
    =?UTF-8?B?UmFmYcWCIE1haiBSYWYyNTY=?=, Apr 5, 2006
    #1
    1. Advertising

  2. =?UTF-8?B?UmFmYcWCIE1haiBSYWYyNTY=?=

    Daniel T. Guest

    In article <e0v34k$6gn$>,
    Rafa©© Maj Raf256 <> wrote:

    > How can I manually (other then
    > http://www.boost.org/libs/smart_ptr/enable_shared_from_this.html)
    > create a shared, and a weak pointer to "this"?
    >
    > Of course I dont want the shared / weak pointer to cause object to be
    > deleted when shared/weak ptr will go out of scope.


    That's the major problem with shaired pointers IMO. I have a class that
    doesn't have this problem. I store the reference counts in a map<void*,
    int>

    That way, both pointer objects in the code below increment the same
    reference count.

    class MyClass {
    public:
    void doThis() {
    Ptr<MyClass> p( this );
    // ...
    }
    };

    void func( Ptr<MyClass> foo ) {
    foo->doThis();
    }

    --
    Magic depends on tradition and belief. It does not welcome observation,
    nor does it profit by experiment. On the other hand, science is based
    on experience; it is open to correction by observation and experiment.
    Daniel T., Apr 5, 2006
    #2
    1. Advertising

  3. =?UTF-8?B?UmFmYcWCIE1haiBSYWYyNTY=?=

    Joe Gottman Guest

    "Rafal Maj Raf256" <> wrote in
    message news:e0v34k$6gn$...
    >
    > How can I manually (other then
    > http://www.boost.org/libs/smart_ptr/enable_shared_from_this.html)
    > create a shared, and a weak pointer to "this"?
    >
    > Of course I dont want the shared / weak pointer to cause object to be
    > deleted when shared/weak ptr will go out of scope.
    >
    >


    If you know that your object is long lived, you can do the following:

    struct null_deleter
    {
    template <class T> void operator()(T *) {}
    }

    Then in your code, just return a shared_ptr<your_type>(this, null_deleter())

    Joe Gottman
    Joe Gottman, Apr 6, 2006
    #3
    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. Raf256
    Replies:
    1
    Views:
    2,221
    Howard Hinnant
    Apr 19, 2005
  2. Goran
    Replies:
    1
    Views:
    555
    mlimber
    Oct 24, 2005
  3. Colin Caughie
    Replies:
    1
    Views:
    713
    Shooting
    Aug 29, 2006
  4. raj s
    Replies:
    4
    Views:
    930
    raj s
    Jul 28, 2008
  5. puzzlecracker
    Replies:
    1
    Views:
    396
    raj s
    Jul 28, 2008
Loading...

Share This Page