cppannotations/yo/stl/definingshared.yo
2009-12-28 16:56:23 +00:00

41 lines
1.8 KiB
Text

There are four ways to hi(shared_ptr: defining) define tt(shared_ptr)
objects. Each definition contains the usual tt(<type>) specifier between
angle brackets:
itemization(
it() The default constructor simply creates a tt(shared_ptr) object that
does not point to a particular block of memory. Its pointer is initialized to
0 (zero):
verb(
shared_ptr<type> identifier;
)
This form is discussed in section ref(SHAREDPLAIN).
it() The copy constructor initializes a tt(shared_ptr) so that both
objects share the memory pointed at by the existing object. The copy
constructor also increments the tt(shared_ptr)'s reference count. Example:
verb(
shared_ptr<string> org(new string("hi there"));
shared_ptr<string> copy(org); // reference count now 2
)
it() The move constructor initializes a tt(shared_ptr) with the pointer
and reference count of a temporary tt(shared_ptr). The temporary
tt(shared_ptr) is changed into a 0-pointer. An existing tt(shared_ptr) may
have its data moved to a newly defined tt(shared_ptr) (turning the existing
tt(shared_ptr) into a 0-pointer as well) by applying
hi(move)tt(std::move). Example:
verb(
shared_ptr<string> grabber(shared_ptr<string>(new string("hi there")));
)
it() The form that is used most often initializes a tt(shared_ptr) object
to the block of dynamically allocated memory that is passed to the object's
constructor. Optionally ti(deleter) can be provided. A (free) function (or
function object) receiving the tt(shared_ptr)'s pointer as its argument can be
passed as deleter. It is supposed to return the dynamically allocated
memory to the common pool (doing nothing if the pointer equals zero).
verb(
shared_ptr<type> identifier (new-expression [, deleter]);
)
This form is discussed in section ref(SHAREDNEW).
)