mirror of
https://gitlab.com/fbb-git/cppannotations
synced 2024-11-18 10:06:54 +01:00
1a79a29b78
git-svn-id: https://cppannotations.svn.sourceforge.net/svnroot/cppannotations/trunk@361 f6dd340e-d3f9-0310-b409-bdd246841980
28 lines
1.4 KiB
Text
28 lines
1.4 KiB
Text
Most often a tt(shared_ptr) hi(shared_ptr: initialization) is initialized by a
|
|
dynamically allocated block of memory. The generic form is:
|
|
verb(
|
|
shared_ptr<type [, deleter_type]> identifier(new-expression
|
|
[, deleter = deleter_type()]);
|
|
)
|
|
The second (template) argument (tt(deleter(_type))) is optional and may
|
|
hi(deleter) refer to a free function or function object handling the
|
|
destruction of the allocated memory. A deleter is used, e.g., in situations
|
|
where a double pointer is allocated and the destruction must visit each nested
|
|
pointer to destroy the allocated memory (see below for an illustration). It
|
|
is used in situations comparable to those encountered with tt(unique_ptr)
|
|
(cf. section ref(UNIQUENEW)).
|
|
|
|
Here is an example initializing an tt(shared_ptr) pointing to a tt(string)
|
|
object:
|
|
verb(
|
|
shared_ptr<string> strPtr(new string("Hello world"));
|
|
)
|
|
The argument that is passed to the constructor is the pointer returned by
|
|
tt(operator new). Note that tt(type) does em(not) mention the pointer. The
|
|
hi(shared_ptr: used type) type that is used in the tt(shared_ptr) construction
|
|
is the same as the type that is used in tt(new) expressions.
|
|
|
|
The example illustrates that two tt(shared_ptrs) indeed share their
|
|
information. After modifying the information controlled by one of the
|
|
objects the information controlled by the other object is modified as well:
|
|
verbinclude(stl/examples/sharedinsert.cc)
|