|
Boost : |
From: dynalink (dynalink_at_[hidden])
Date: 2003-02-10 12:13:00
I need to create a single instance of X and have it disappear when the
final B pointer goes out of scope. The problem with this code is that A::m_X
immediately bumps the ref count and only goes out of scope on termination.
Here's a simple example:
class X; // defined elsewhere; in my case wraps a classic C structure.
typedef boost::smart_ptr<X> X_ptr;
struct A {
static X_ptr create() { if (!m_X.get()) m_X.reset(new X); return m_X; }
static X_ptr m_X;
};
struct B {
B() : m_A(A::create()){}
X_ptr m_A;
};
int main (int, void** argv)
{
B a;
B b;
B c;
do_something();
}
Thanks,
Peter.
Peter Krnjevic
Dynalink Technologies
"Peter Dimov" <pdimov_at_[hidden]> wrote in message
news:002801c2d122$5d604a40$1d00a8c0_at_pdimov2...
> dynalink wrote:
> > Forgive me if this seems like a really basic question, but how does
> > one create a shared_ptr with an initial use count of zero?
>
> There is no way to create a shared_ptr with use_count of zero. Use_count()
> returns the number of shared_ptr instances sharing ownership. Use_count()
==
> 0 means that there are is no shared_ptr instance. If you are able to call
> shared_ptr::use_count(), you have a(t least one) shared_ptr.
>
> Zero can only be returned from weak_ptr::use_count(), and this means that
> all shared_ptr's have been destroyed.
>
> Why do you ask?
>
> _______________________________________________
> Unsubscribe & other changes:
http://lists.boost.org/mailman/listinfo.cgi/boost
>
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk