summaryrefslogtreecommitdiff
path: root/documentation/content/xdocs/SharedPtr.html
blob: 31effc4389b4818df01387538a443d6b5219c508 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<html>
    <head>
        <title>Apache Qpid : SharedPtr</title>
	    <link rel="stylesheet" href="styles/site.css" type="text/css" />
        <META http-equiv="Content-Type" content="text/html; charset=UTF-8">	    
    </head>

    <body>
	    <table class="pagecontent" border="0" cellpadding="0" cellspacing="0" width="100%" bgcolor="#ffffff">
		    <tr>
			    <td valign="top" class="pagebody">
				    <div class="pageheader">
					    <span class="pagetitle">
                            Apache Qpid : SharedPtr
                                                    </span>
				    </div>
				    <div class="pagesubheading">
					    This page last changed on Oct 19, 2006 by <font color="#0050B2">mmccorma</font>.
				    </div>

				    <p>std::tr1::shared_ptr is an almost-standard smart pointer template that<br/>
provides unintrusive reference-counting semantics for any class. It<br/>
almost makes memory management too easy for a C++ programmer.</p>

<p>It's available in g++ and some other compilers by default. There are<br/>
several open source implementations if we ever port to a compiler that<br/>
doesn't have it.</p>

<p>The golde rule: if class Foo has shared ownership then never <em>ever</em><br/>
write <tt>Foo*</tt>. Anywhere. Ever. Use shared_ptr in all function<br/>
signatures and variables, use std::tr1::dynamic_pointer_cast and<br/>
friends for casting.</p>

<p>Qpid will use it for all classes with shared ownership semantics,<br/>
enforced by private constructors and static factory functions. We'll<br/>
also adopt the convention to typedef shared_ptr within the class for<br/>
convenience. E.g.</p>

<div class="code"><div class="codeContent">
<pre class="code-java">class Foo {
   Foo() { ... }

 <span class="code-keyword">public</span>:
   typedef std::tr1::shared_ptr&lt;Foo&gt; shared_ptr;
   <span class="code-keyword">static</span> shared_ptr create() { <span class="code-keyword">return</span> <span class="code-keyword">new</span> Foo() }
   <span class="code-comment">// .. a create <span class="code-keyword">for</span> each constructor.
</span>}

Foo::shared_ptr p = Foo::create();  <span class="code-comment">// etc...</span></pre>
</div></div>


<p>There's a good article at <a href="http://www.boost.org/libs/smart_ptr/sp_techniques.html" title="Visit page outside Confluence">http://www.boost.org/libs/smart_ptr/sp_techniques.html</a>.</p>




				    
                    			    </td>
		    </tr>
	    </table>
	    <table border="0" cellpadding="0" cellspacing="0" width="100%">
			<tr>
				<td height="12" background="border/border_bottom.gif"><img src="border/spacer.gif" width="1" height="1" border="0"/></td>
			</tr>
		    <tr>
			    <td align="center"><font color="grey">Document generated by Confluence on Apr 22, 2008 02:47</font></td>
		    </tr>
	    </table>
    </body>
</html>