批註:智慧指標分析
fdsafwagdagadg6576發表於2016-06-22
<code class="cpp" style="font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; padding: 0px; color: inherit; background-color: transparent;"><span class="com" style="color: rgb(147, 161, 161);">#include</span><span class="pln" style="color: rgb(102, 217, 239);"> </span><span class="str" style="color: rgb(230, 219, 116);"><iostream></span><span class="pln" style="color: rgb(102, 217, 239);"> </span><span class="com" style="color: rgb(147, 161, 161);">#include</span><span class="pln" style="color: rgb(102, 217, 239);"> </span><span class="str" style="color: rgb(230, 219, 116);"><windows.h></span><span class="pln" style="color: rgb(102, 217, 239);"> </span><span class="kwd" style="color: rgb(249, 38, 89);">using</span><span class="pln" style="color: rgb(102, 217, 239);"> </span><span class="kwd" style="color: rgb(249, 38, 89);">namespace</span><span class="pln" style="color: rgb(102, 217, 239);"> std</span><span class="pun" style="color: rgb(248, 248, 242);">;</span><span class="pln" style="color: rgb(102, 217, 239);"> </span><span class="com" style="color: rgb(147, 161, 161);">#define</span><span class="pln" style="color: rgb(102, 217, 239);"> SAFE_DELETE</span><span class="pun" style="color: rgb(248, 248, 242);">(</span><span class="pln" style="color: rgb(102, 217, 239);">p</span><span class="pun" style="color: rgb(248, 248, 242);">)</span><span class="pln" style="color: rgb(102, 217, 239);"> </span><span class="kwd" style="color: rgb(249, 38, 89);">if</span><span class="pln" style="color: rgb(102, 217, 239);"> </span><span class="pun" style="color: rgb(248, 248, 242);">(</span><span class="pln" style="color: rgb(102, 217, 239);">p</span><span class="pun" style="color: rgb(248, 248, 242);">)</span><span class="pln" style="color: rgb(102, 217, 239);"> </span><span class="pun" style="color: rgb(248, 248, 242);">{</span><span class="pln" style="color: rgb(102, 217, 239);"> </span><span class="kwd" style="color: rgb(249, 38, 89);">delete</span><span class="pln" style="color: rgb(102, 217, 239);"> p</span><span class="pun" style="color: rgb(248, 248, 242);">;</span><span class="pln" style="color: rgb(102, 217, 239);"> p </span><span class="pun" style="color: rgb(248, 248, 242);">=</span><span class="pln" style="color: rgb(102, 217, 239);"> NULL</span><span class="pun" style="color: rgb(248, 248, 242);">;</span><span class="pln" style="color: rgb(102, 217, 239);"> </span><span class="pun" style="color: rgb(248, 248, 242);">}</span><span class="pln" style="color: rgb(102, 217, 239);"> </span><span class="kwd" style="color: rgb(249, 38, 89);">class</span><span class="pln" style="color: rgb(102, 217, 239);"> </span><span class="typ" style="color: rgb(166, 226, 46);">KRefCount</span><span class="pln" style="color: rgb(102, 217, 239);"> </span><span class="pun" style="color: rgb(248, 248, 242);">{</span><span class="pln" style="color: rgb(102, 217, 239);"> </span><span class="kwd" style="color: rgb(249, 38, 89);">public</span><span class="pun" style="color: rgb(248, 248, 242);">:</span><span class="pln" style="color: rgb(102, 217, 239);"> </span><span class="typ" style="color: rgb(166, 226, 46);">KRefCount</span><span class="pun" style="color: rgb(248, 248, 242);">():</span><span class="pln" style="color: rgb(102, 217, 239);">m_nCount</span><span class="pun" style="color: rgb(248, 248, 242);">(</span><span class="lit" style="color: rgb(174, 129, 255);">0</span><span class="pun" style="color: rgb(248, 248, 242);">){}</span><span class="pln" style="color: rgb(102, 217, 239);"> </span><span class="kwd" style="color: rgb(249, 38, 89);">public</span><span class="pun" style="color: rgb(248, 248, 242);">:</span></code>
<code class="cpp" style="font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; padding: 0px; background-color: transparent;"><span class="pln"> <span style="color:#ffff33;">//notes:</span><span style="color:#ffff00;">對引用增加1</span> </span><span class="kwd" style="color: rgb(249, 38, 89);">unsigned</span><span class="pln" style="color: rgb(102, 217, 239);"> </span><span class="typ" style="color: rgb(166, 226, 46);">AddRef</span><span class="pun" style="color: rgb(248, 248, 242);">(){</span><span class="pln" style="color: rgb(102, 217, 239);"> </span><span class="kwd" style="color: rgb(249, 38, 89);">return</span><span class="pln" style="color: rgb(102, 217, 239);"> </span><span class="typ" style="color: rgb(166, 226, 46);">InterlockedIncrement</span><span class="pun" style="color: rgb(248, 248, 242);">(&</span><span class="pln" style="color: rgb(102, 217, 239);">m_nCount</span><span class="pun" style="color: rgb(248, 248, 242);">);</span><span class="pln" style="color: rgb(102, 217, 239);"> </span><span class="pun" style="color: rgb(248, 248, 242);">}</span><span class="pln" style="color: rgb(102, 217, 239);"> </span><span class="kwd" style="color: rgb(249, 38, 89);">unsigned</span><span class="pln" style="color: rgb(102, 217, 239);"> </span><span class="typ" style="color: rgb(166, 226, 46);">Release</span><span class="pun" style="color: rgb(248, 248, 242);">(){</span><span class="pln" style="color: rgb(102, 217, 239);"> </span><span class="kwd" style="color: rgb(249, 38, 89);">return</span><span class="pln" style="color: rgb(102, 217, 239);"> </span><span class="typ" style="color: rgb(166, 226, 46);">InterlockedDecrement</span><span class="pun" style="color: rgb(248, 248, 242);">(&</span><span class="pln" style="color: rgb(102, 217, 239);">m_nCount</span><span class="pun" style="color: rgb(248, 248, 242);">);</span><span class="pln" style="color: rgb(102, 217, 239);"> </span><span class="pun" style="color: rgb(248, 248, 242);">}</span><span class="pln" style="color: rgb(102, 217, 239);"> </span><span class="kwd" style="color: rgb(249, 38, 89);">void</span><span class="pln" style="color: rgb(102, 217, 239);"> </span><span class="typ" style="color: rgb(166, 226, 46);">Reset</span><span class="pun" style="color: rgb(248, 248, 242);">(){</span><span class="pln" style="color: rgb(102, 217, 239);"> m_nCount </span><span class="pun" style="color: rgb(248, 248, 242);">=</span><span class="pln" style="color: rgb(102, 217, 239);"> </span><span class="lit" style="color: rgb(174, 129, 255);">0</span><span class="pun" style="color: rgb(248, 248, 242);">;</span><span class="pln" style="color: rgb(102, 217, 239);"> </span><span class="pun" style="color: rgb(248, 248, 242);">}</span><span class="pln" style="color: rgb(102, 217, 239);"> </span><span class="kwd" style="color: rgb(249, 38, 89);">private</span><span class="pun" style="color: rgb(248, 248, 242);">:</span><span class="pln" style="color: rgb(102, 217, 239);"> </span><span class="kwd" style="color: rgb(249, 38, 89);">unsigned</span><span class="pln" style="color: rgb(102, 217, 239);"> </span><span class="kwd" style="color: rgb(249, 38, 89);">long</span><span class="pln" style="color: rgb(102, 217, 239);"> m_nCount</span><span class="pun" style="color: rgb(248, 248, 242);">;</span><span class="pln" style="color: rgb(102, 217, 239);"> </span><span class="pun" style="color: rgb(248, 248, 242);">};</span><span class="pln" style="color: rgb(102, 217, 239);"> //T 表示型別,T可以是class,可以是指標 etc </span><span class="kwd" style="color: rgb(249, 38, 89);">template</span><span class="pln" style="color: rgb(102, 217, 239);"> </span><span class="pun" style="color: rgb(248, 248, 242);"><</span><span class="kwd" style="color: rgb(249, 38, 89);">typename</span><span class="pln" style="color: rgb(102, 217, 239);"> T</span><span class="pun" style="color: rgb(248, 248, 242);">></span><span class="pln" style="color: rgb(102, 217, 239);"> </span><span class="kwd" style="color: rgb(249, 38, 89);">class</span><span class="pln" style="color: rgb(102, 217, 239);"> </span><span class="typ" style="color: rgb(166, 226, 46);">SmartPtr</span><span class="pln" style="color: rgb(102, 217, 239);"> </span><span class="pun" style="color: rgb(248, 248, 242);">{</span><span class="pln" style="color: rgb(102, 217, 239);"> </span><span class="kwd" style="color: rgb(249, 38, 89);">public</span><span class="pun" style="color: rgb(248, 248, 242);">:</span><span class="pln" style="color: rgb(102, 217, 239);"> </span><span class="typ" style="color: rgb(166, 226, 46);">SmartPtr</span><span class="pun" style="color: rgb(248, 248, 242);">(</span><span class="kwd" style="color: rgb(249, 38, 89);">void</span><span class="pun" style="color: rgb(248, 248, 242);">)</span><span class="pln" style="color: rgb(102, 217, 239);"> </span><span class="pun" style="color: rgb(248, 248, 242);">:</span><span class="pln" style="color: rgb(102, 217, 239);"> m_pData</span><span class="pun" style="color: rgb(248, 248, 242);">(</span><span class="pln" style="color: rgb(102, 217, 239);">NULL</span><span class="pun" style="color: rgb(248, 248, 242);">)</span><span class="pln" style="color: rgb(102, 217, 239);"> </span><span class="pun" style="color: rgb(248, 248, 242);">{</span><span class="pln" style="color: rgb(102, 217, 239);"> m_pReference </span><span class="pun" style="color: rgb(248, 248, 242);">=</span><span class="pln" style="color: rgb(102, 217, 239);"> </span><span class="kwd" style="color: rgb(249, 38, 89);">new</span><span class="pln" style="color: rgb(102, 217, 239);"> </span><span class="typ" style="color: rgb(166, 226, 46);">KRefCount</span><span class="pun" style="color: rgb(248, 248, 242);">();</span><span class="pln" style="color: rgb(102, 217, 239);"> m_pReference</span><span class="pun" style="color: rgb(248, 248, 242);">-></span><span class="typ" style="color: rgb(166, 226, 46);">AddRef</span><span class="pun" style="color: rgb(248, 248, 242);">();</span><span class="pln" style="color: rgb(102, 217, 239);"> </span><span class="pun" style="color: rgb(248, 248, 242);">}</span><span class="pln" style="color: rgb(102, 217, 239);"> //引數是指標.新new一個物件</span></code>
<code class="cpp" style="font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; padding: 0px; background-color: transparent;"><span class="pln" style="color: rgb(102, 217, 239);"><span class="pln" style="background-color: transparent;"> </span><span class="typ" style="color: rgb(166, 226, 46); background-color: transparent;">SmartPtr</span><span class="pun" style="color: rgb(248, 248, 242); background-color: transparent;">(</span><span class="pln" style="background-color: transparent;">T</span><span class="pun" style="color: rgb(248, 248, 242); background-color: transparent;">*</span><span class="pln" style="background-color: transparent;"> pValue</span><span class="pun" style="color: rgb(248, 248, 242); background-color: transparent;">)</span> </span><span class="pln" style="color: rgb(102, 217, 239);"> </span><span class="pun" style="color: rgb(248, 248, 242);">:</span><span class="pln" style="color: rgb(102, 217, 239);"> m_pData</span><span class="pun" style="color: rgb(248, 248, 242);">(</span><span class="pln" style="color: rgb(102, 217, 239);">pValue</span><span class="pun" style="color: rgb(248, 248, 242);">)</span><span class="pln" style="color: rgb(102, 217, 239);"> </span><span class="pun" style="color: rgb(248, 248, 242);">{</span></code>
<code class="cpp" style="font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; padding: 0px; background-color: transparent;"><span class="pln"> <span style="color:#66cccc;">m_pReference </span></span><span class="pun" style="color: rgb(248, 248, 242);">=</span><span class="pln" style="color: rgb(102, 217, 239);"> </span><span class="kwd" style="color: rgb(249, 38, 89);">new</span><span class="pln" style="color: rgb(102, 217, 239);"> </span><span class="typ" style="color: rgb(166, 226, 46);">KRefCount</span><span class="pun" style="color: rgb(248, 248, 242);">();</span><span class="pln" style="color: rgb(102, 217, 239);"> m_pReference</span><span class="pun" style="color: rgb(248, 248, 242);">-></span><span class="typ" style="color: rgb(166, 226, 46);">AddRef</span><span class="pun" style="color: rgb(248, 248, 242);">();</span><span class="pln" style="color: rgb(102, 217, 239);"> </span><span class="pun" style="color: rgb(248, 248, 242);">}</span><span class="pln" style="color: rgb(102, 217, 239);"> //引數是模板 //copy建構函式,這是一個淺拷貝.但是執行了AddRef.即當a(b),則a,b都引用了 </span><span class="typ" style="color: rgb(166, 226, 46);">SmartPtr</span><span class="pun" style="color: rgb(248, 248, 242);">(</span><span class="kwd" style="color: rgb(249, 38, 89);">const</span><span class="pln" style="color: rgb(102, 217, 239);"> </span><span class="typ" style="color: rgb(166, 226, 46);">SmartPtr</span><span class="pun" style="color: rgb(248, 248, 242);"><</span><span class="pln" style="color: rgb(102, 217, 239);">T</span><span class="pun" style="color: rgb(248, 248, 242);">>&</span><span class="pln" style="color: rgb(102, 217, 239);"> sp</span><span class="pun" style="color: rgb(248, 248, 242);">)</span><span class="pln" style="color: rgb(102, 217, 239);"> </span><span class="pun" style="color: rgb(248, 248, 242);">:</span><span class="pln" style="color: rgb(102, 217, 239);"> m_pData</span><span class="pun" style="color: rgb(248, 248, 242);">(</span><span class="pln" style="color: rgb(102, 217, 239);">sp</span><span class="pun" style="color: rgb(248, 248, 242);">.</span><span class="pln" style="color: rgb(102, 217, 239);">m_pData</span><span class="pun" style="color: rgb(248, 248, 242);">)</span><span class="pln" style="color: rgb(102, 217, 239);"> </span><span class="pun" style="color: rgb(248, 248, 242);">,</span><span class="pln" style="color: rgb(102, 217, 239);"> m_pReference</span><span class="pun" style="color: rgb(248, 248, 242);">(</span><span class="pln" style="color: rgb(102, 217, 239);">sp</span><span class="pun" style="color: rgb(248, 248, 242);">.</span><span class="pln" style="color: rgb(102, 217, 239);">m_pReference</span><span class="pun" style="color: rgb(248, 248, 242);">)</span><span class="pln" style="color: rgb(102, 217, 239);"> </span><span class="pun" style="color: rgb(248, 248, 242);">{</span><span class="pln" style="color: rgb(102, 217, 239);"> m_pReference</span><span class="pun" style="color: rgb(248, 248, 242);">-></span><span class="typ" style="color: rgb(166, 226, 46);">AddRef</span><span class="pun" style="color: rgb(248, 248, 242);">();</span><span class="pln" style="color: rgb(102, 217, 239);"> </span><span class="pun" style="color: rgb(248, 248, 242);">}</span><span class="pln" style="color: rgb(102, 217, 239);"> </span><span class="pun" style="color: rgb(248, 248, 242);">~</span><span class="typ" style="color: rgb(166, 226, 46);">SmartPtr</span><span class="pun" style="color: rgb(248, 248, 242);">(</span><span class="kwd" style="color: rgb(249, 38, 89);">void</span><span class="pun" style="color: rgb(248, 248, 242);">)</span><span class="pln" style="color: rgb(102, 217, 239);"> </span><span class="pun" style="color: rgb(248, 248, 242);">{</span></code>
<code class="cpp" style="font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; padding: 0px; background-color: transparent;"><span class="pln"><span style="color:#ffff33;"> //當引用不存在,但是release-1可能不等於0,其他的refer還在,不能刪除。</span></span></code>
<code class="cpp" style="font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; padding: 0px; background-color: transparent;"><span class="pln"><span style="color:#ffff33;"> //只有當引用沒有,同時releasee為0才delete</span> </span><span class="kwd" style="color: rgb(249, 38, 89);">if</span><span class="pln" style="color: rgb(102, 217, 239);"> </span><span class="pun" style="color: rgb(248, 248, 242);">(</span><span class="pln" style="color: rgb(102, 217, 239);">m_pReference </span><span class="pun" style="color: rgb(248, 248, 242);">&&</span><span class="pln" style="color: rgb(102, 217, 239);"> m_pReference</span><span class="pun" style="color: rgb(248, 248, 242);">-></span><span class="typ" style="color: rgb(166, 226, 46);">Release</span><span class="pun" style="color: rgb(248, 248, 242);">()</span><span class="pln" style="color: rgb(102, 217, 239);"> </span><span class="pun" style="color: rgb(248, 248, 242);">==</span><span class="pln" style="color: rgb(102, 217, 239);"> </span><span class="lit" style="color: rgb(174, 129, 255);">0</span><span class="pun" style="color: rgb(248, 248, 242);">)</span><span class="pln" style="color: rgb(102, 217, 239);"> </span><span class="pun" style="color: rgb(248, 248, 242);">{</span><span class="pln" style="color: rgb(102, 217, 239);"> SAFE_DELETE</span><span class="pun" style="color: rgb(248, 248, 242);">(</span><span class="pln" style="color: rgb(102, 217, 239);">m_pData</span><span class="pun" style="color: rgb(248, 248, 242);">);</span><span class="pln" style="color: rgb(102, 217, 239);"> SAFE_DELETE</span><span class="pun" style="color: rgb(248, 248, 242);">(</span><span class="pln" style="color: rgb(102, 217, 239);">m_pReference</span><span class="pun" style="color: rgb(248, 248, 242);">);</span><span class="pln" style="color: rgb(102, 217, 239);"> </span><span class="pun" style="color: rgb(248, 248, 242);">}</span><span class="pln" style="color: rgb(102, 217, 239);"> </span><span class="pun" style="color: rgb(248, 248, 242);">}</span><span class="pln" style="color: rgb(102, 217, 239);"> //過載了*,返回了引用. 目的向m_pData賦值</span></code>
<code class="cpp" style="font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; padding: 0px; background-color: transparent;"><span class="pln" style="color: rgb(102, 217, 239);"> p(0x1234)-------------*p=1; &(*m_pDdata)(0x1234)---------------*m_pData=1;</span></code>
<code class="cpp" style="font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; padding: 0px; background-color: transparent;"><span class="pln" style="color: rgb(102, 217, 239);"> </span></code><span style="color: rgb(102, 217, 239); font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; background-color: transparent;"> </span><span class="pln" style="font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; color: rgb(102, 217, 239); background-color: transparent;"> </span><span class="kwd" style="font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; color: rgb(249, 38, 89); background-color: transparent;">inline</span><span class="pln" style="font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; color: rgb(102, 217, 239); background-color: transparent;"> T</span><span class="pun" style="font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; color: rgb(248, 248, 242); background-color: transparent;">&</span><span class="pln" style="font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; color: rgb(102, 217, 239); background-color: transparent;"> </span><span class="kwd" style="font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; color: rgb(249, 38, 89); background-color: transparent;">operator</span><span class="pun" style="font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; color: rgb(248, 248, 242); background-color: transparent;">*()</span>
<code class="cpp" style="font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; padding: 0px; background-color: transparent;"><span class="pln" style="color: rgb(102, 217, 239);"> </span><span class="pun" style="color: rgb(248, 248, 242);">{</span><span class="pln" style="color: rgb(102, 217, 239);"> </span><span class="kwd" style="color: rgb(249, 38, 89);">return</span><span class="pln" style="color: rgb(102, 217, 239);"> </span><span class="pun" style="color: rgb(248, 248, 242);">*</span><span class="pln" style="color: rgb(102, 217, 239);">m_pData</span><span class="pun" style="color: rgb(248, 248, 242);">;</span><span class="pln" style="color: rgb(102, 217, 239);"> </span><span class="pun" style="color: rgb(248, 248, 242);">}</span><span class="pln" style="color: rgb(102, 217, 239);"> </span><span class="kwd" style="color: rgb(249, 38, 89);">inline</span><span class="pln" style="color: rgb(102, 217, 239);"> T</span><span class="pun" style="color: rgb(248, 248, 242);">*</span><span class="pln" style="color: rgb(102, 217, 239);"> </span><span class="kwd" style="color: rgb(249, 38, 89);">operator</span><span class="pun" style="color: rgb(248, 248, 242);">->()</span><span class="pln" style="color: rgb(102, 217, 239);"> </span><span class="pun" style="color: rgb(248, 248, 242);">{</span><span class="pln" style="color: rgb(102, 217, 239);"> </span><span class="kwd" style="color: rgb(249, 38, 89);">return</span><span class="pln" style="color: rgb(102, 217, 239);"> m_pData</span><span class="pun" style="color: rgb(248, 248, 242);">;</span><span class="pln" style="color: rgb(102, 217, 239);"> </span><span class="pun" style="color: rgb(248, 248, 242);">}</span><span class="pln" style="color: rgb(102, 217, 239);"> //copy 賦值函式。淺賦值。和copy函式一樣。 增加了refer技術+1功能。 </span><span class="typ" style="color: rgb(166, 226, 46);">SmartPtr</span><span class="pun" style="color: rgb(248, 248, 242);"><</span><span class="pln" style="color: rgb(102, 217, 239);">T</span><span class="pun" style="color: rgb(248, 248, 242);">>&</span><span class="pln" style="color: rgb(102, 217, 239);"> </span><span class="kwd" style="color: rgb(249, 38, 89);">operator</span><span class="pun" style="color: rgb(248, 248, 242);">=(</span><span class="kwd" style="color: rgb(249, 38, 89);">const</span><span class="pln" style="color: rgb(102, 217, 239);"> </span><span class="typ" style="color: rgb(166, 226, 46);">SmartPtr</span><span class="pun" style="color: rgb(248, 248, 242);"><</span><span class="pln" style="color: rgb(102, 217, 239);">T</span><span class="pun" style="color: rgb(248, 248, 242);">>&</span><span class="pln" style="color: rgb(102, 217, 239);"> sp</span><span class="pun" style="color: rgb(248, 248, 242);">)</span><span class="pln" style="color: rgb(102, 217, 239);"> </span><span class="pun" style="color: rgb(248, 248, 242);">{</span><span class="pln" style="color: rgb(102, 217, 239);"> </span><span class="kwd" style="color: rgb(249, 38, 89);">if</span><span class="pln" style="color: rgb(102, 217, 239);"> </span><span class="pun" style="color: rgb(248, 248, 242);">(</span><span class="kwd" style="color: rgb(249, 38, 89);">this</span><span class="pln" style="color: rgb(102, 217, 239);"> </span><span class="pun" style="color: rgb(248, 248, 242);">!=</span><span class="pln" style="color: rgb(102, 217, 239);"> </span><span class="pun" style="color: rgb(248, 248, 242);">&</span><span class="pln" style="color: rgb(102, 217, 239);">sp</span><span class="pun" style="color: rgb(248, 248, 242);">)</span><span class="pln" style="color: rgb(102, 217, 239);"> </span><span class="pun" style="color: rgb(248, 248, 242);">{</span><span class="pln" style="color: rgb(102, 217, 239);"> </span><span class="kwd" style="color: rgb(249, 38, 89);">if</span><span class="pln" style="color: rgb(102, 217, 239);"> </span><span class="pun" style="color: rgb(248, 248, 242);">(</span><span class="pln" style="color: rgb(102, 217, 239);">m_pReference </span><span class="pun" style="color: rgb(248, 248, 242);">&&</span><span class="pln" style="color: rgb(102, 217, 239);"> m_pReference</span><span class="pun" style="color: rgb(248, 248, 242);">-></span><span class="typ" style="color: rgb(166, 226, 46);">Release</span><span class="pun" style="color: rgb(248, 248, 242);">()</span><span class="pln" style="color: rgb(102, 217, 239);"> </span><span class="pun" style="color: rgb(248, 248, 242);">==</span><span class="pln" style="color: rgb(102, 217, 239);"> </span><span class="lit" style="color: rgb(174, 129, 255);">0</span><span class="pun" style="color: rgb(248, 248, 242);">)</span><span class="pln" style="color: rgb(102, 217, 239);"> </span><span class="pun" style="color: rgb(248, 248, 242);">{</span><span class="pln" style="color: rgb(102, 217, 239);"> SAFE_DELETE</span><span class="pun" style="color: rgb(248, 248, 242);">(</span><span class="pln" style="color: rgb(102, 217, 239);">m_pData</span><span class="pun" style="color: rgb(248, 248, 242);">);</span><span class="pln" style="color: rgb(102, 217, 239);"> SAFE_DELETE</span><span class="pun" style="color: rgb(248, 248, 242);">(</span><span class="pln" style="color: rgb(102, 217, 239);">m_pReference</span><span class="pun" style="color: rgb(248, 248, 242);">);</span><span class="pln" style="color: rgb(102, 217, 239);"> </span><span class="pun" style="color: rgb(248, 248, 242);">}</span><span class="pln" style="color: rgb(102, 217, 239);"> m_pData </span><span class="pun" style="color: rgb(248, 248, 242);">=</span><span class="pln" style="color: rgb(102, 217, 239);"> sp</span><span class="pun" style="color: rgb(248, 248, 242);">.</span><span class="pln" style="color: rgb(102, 217, 239);">m_pData</span><span class="pun" style="color: rgb(248, 248, 242);">;</span><span class="pln" style="color: rgb(102, 217, 239);"> m_pReference </span><span class="pun" style="color: rgb(248, 248, 242);">=</span><span class="pln" style="color: rgb(102, 217, 239);"> sp</span><span class="pun" style="color: rgb(248, 248, 242);">.</span><span class="pln" style="color: rgb(102, 217, 239);">m_pReference</span><span class="pun" style="color: rgb(248, 248, 242);">;</span><span class="pln" style="color: rgb(102, 217, 239);"> m_pReference</span><span class="pun" style="color: rgb(248, 248, 242);">-></span><span class="typ" style="color: rgb(166, 226, 46);">AddRef</span><span class="pun" style="color: rgb(248, 248, 242);">();</span><span class="pln" style="color: rgb(102, 217, 239);"> </span><span class="pun" style="color: rgb(248, 248, 242);">}</span><span class="pln" style="color: rgb(102, 217, 239);"> </span><span class="kwd" style="color: rgb(249, 38, 89);">return</span><span class="pln" style="color: rgb(102, 217, 239);"> </span><span class="pun" style="color: rgb(248, 248, 242);">*</span><span class="kwd" style="color: rgb(249, 38, 89);">this</span><span class="pun" style="color: rgb(248, 248, 242);">;</span><span class="pln" style="color: rgb(102, 217, 239);"> </span><span class="pun" style="color: rgb(248, 248, 242);">}</span><span class="pln" style="color: rgb(102, 217, 239);"> //實現了深賦值 </span><span class="typ" style="color: rgb(166, 226, 46);">SmartPtr</span><span class="pun" style="color: rgb(248, 248, 242);"><</span><span class="pln" style="color: rgb(102, 217, 239);">T</span><span class="pun" style="color: rgb(248, 248, 242);">>&</span><span class="pln" style="color: rgb(102, 217, 239);"> </span><span class="kwd" style="color: rgb(249, 38, 89);">operator</span><span class="pun" style="color: rgb(248, 248, 242);">=(</span><span class="pln" style="color: rgb(102, 217, 239);">T</span><span class="pun" style="color: rgb(248, 248, 242);">*</span><span class="pln" style="color: rgb(102, 217, 239);"> pValue</span><span class="pun" style="color: rgb(248, 248, 242);">)</span><span class="pln" style="color: rgb(102, 217, 239);"> </span><span class="pun" style="color: rgb(248, 248, 242);">{</span><span class="pln" style="color: rgb(102, 217, 239);"> </span><span class="kwd" style="color: rgb(249, 38, 89);">if</span><span class="pln" style="color: rgb(102, 217, 239);"> </span><span class="pun" style="color: rgb(248, 248, 242);">(</span><span class="pln" style="color: rgb(102, 217, 239);">m_pReference </span><span class="pun" style="color: rgb(248, 248, 242);">&&</span><span class="pln" style="color: rgb(102, 217, 239);"> m_pReference</span><span class="pun" style="color: rgb(248, 248, 242);">-></span><span class="typ" style="color: rgb(166, 226, 46);">Release</span><span class="pun" style="color: rgb(248, 248, 242);">()</span><span class="pln" style="color: rgb(102, 217, 239);"> </span><span class="pun" style="color: rgb(248, 248, 242);">==</span><span class="pln" style="color: rgb(102, 217, 239);"> </span><span class="lit" style="color: rgb(174, 129, 255);">0</span><span class="pun" style="color: rgb(248, 248, 242);">)</span><span class="pln" style="color: rgb(102, 217, 239);"> </span><span class="pun" style="color: rgb(248, 248, 242);">{</span><span class="pln" style="color: rgb(102, 217, 239);"> SAFE_DELETE</span><span class="pun" style="color: rgb(248, 248, 242);">(</span><span class="pln" style="color: rgb(102, 217, 239);">m_pData</span><span class="pun" style="color: rgb(248, 248, 242);">);</span><span class="pln" style="color: rgb(102, 217, 239);"> SAFE_DELETE</span><span class="pun" style="color: rgb(248, 248, 242);">(</span><span class="pln" style="color: rgb(102, 217, 239);">m_pReference</span><span class="pun" style="color: rgb(248, 248, 242);">);</span><span class="pln" style="color: rgb(102, 217, 239);"> </span><span class="pun" style="color: rgb(248, 248, 242);">}</span><span class="pln" style="color: rgb(102, 217, 239);"> m_pData </span><span class="pun" style="color: rgb(248, 248, 242);">=</span><span class="pln" style="color: rgb(102, 217, 239);"> pValue</span><span class="pun" style="color: rgb(248, 248, 242);">;</span><span class="pln" style="color: rgb(102, 217, 239);"> m_pReference </span><span class="pun" style="color: rgb(248, 248, 242);">=</span><span class="pln" style="color: rgb(102, 217, 239);"> </span><span class="kwd" style="color: rgb(249, 38, 89);">new</span><span class="pln" style="color: rgb(102, 217, 239);"> </span><span class="typ" style="color: rgb(166, 226, 46);">KRefCount</span><span class="pun" style="color: rgb(248, 248, 242);">;</span><span class="pln" style="color: rgb(102, 217, 239);"> m_pReference</span><span class="pun" style="color: rgb(248, 248, 242);">-></span><span class="typ" style="color: rgb(166, 226, 46);">AddRef</span><span class="pun" style="color: rgb(248, 248, 242);">();</span><span class="pln" style="color: rgb(102, 217, 239);"> </span><span class="kwd" style="color: rgb(249, 38, 89);">return</span><span class="pln" style="color: rgb(102, 217, 239);"> </span><span class="pun" style="color: rgb(248, 248, 242);">*</span><span class="kwd" style="color: rgb(249, 38, 89);">this</span><span class="pun" style="color: rgb(248, 248, 242);">;</span><span class="pln" style="color: rgb(102, 217, 239);"> </span><span class="pun" style="color: rgb(248, 248, 242);">}</span><span class="pln" style="color: rgb(102, 217, 239);"> T</span><span class="pun" style="color: rgb(248, 248, 242);">*</span><span class="pln" style="color: rgb(102, 217, 239);"> </span><span class="typ" style="color: rgb(166, 226, 46);">Get</span><span class="pun" style="color: rgb(248, 248, 242);">()</span><span class="pln" style="color: rgb(102, 217, 239);"> </span><span class="pun" style="color: rgb(248, 248, 242);">{</span></code>
<code class="cpp" style="font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; padding: 0px; background-color: transparent;"><span class="pln"> <span style="color:#ffff33;"> //debo comments: why 不直接return m_pData,而是用個臨時標量呢?感覺是一樣的</span> <span style="color:#33ccff;">T</span></span><span class="pun" style="color: rgb(248, 248, 242);">*</span><span class="pln" style="color: rgb(102, 217, 239);"> ptr </span><span class="pun" style="color: rgb(248, 248, 242);">=</span><span class="pln" style="color: rgb(102, 217, 239);"> NULL</span><span class="pun" style="color: rgb(248, 248, 242);">;</span><span class="pln" style="color: rgb(102, 217, 239);"> ptr </span><span class="pun" style="color: rgb(248, 248, 242);">=</span><span class="pln" style="color: rgb(102, 217, 239);"> m_pData</span><span class="pun" style="color: rgb(248, 248, 242);">;</span><span class="pln" style="color: rgb(102, 217, 239);"> </span><span class="kwd" style="color: rgb(249, 38, 89);">return</span><span class="pln" style="color: rgb(102, 217, 239);"> ptr</span><span class="pun" style="color: rgb(248, 248, 242);">;</span><span class="pln" style="color: rgb(102, 217, 239);"> </span><span class="pun" style="color: rgb(248, 248, 242);">}</span><span class="pln" style="color: rgb(102, 217, 239);"> </span><span class="kwd" style="color: rgb(249, 38, 89);">void</span><span class="pln" style="color: rgb(102, 217, 239);"> </span><span class="typ" style="color: rgb(166, 226, 46);">Attach</span><span class="pun" style="color: rgb(248, 248, 242);">(</span><span class="pln" style="color: rgb(102, 217, 239);">T</span><span class="pun" style="color: rgb(248, 248, 242);">*</span><span class="pln" style="color: rgb(102, 217, 239);"> pObject</span><span class="pun" style="color: rgb(248, 248, 242);">)</span><span class="pln" style="color: rgb(102, 217, 239);"> </span><span class="pun" style="color: rgb(248, 248, 242);">{</span><span style="color:#ffff00;"><span class="pun">//這個地方有個bug</span><span class="pun">.應該判斷是不是,if(m_pReference) ,不是從業務邏輯判斷需不需要,而是從實現功能看,if m_Reference is null, null->Release coredump</span></span><span class="pun"><span style="color:#f8f8f2;"> </span></span><span class="pln" style="color: rgb(102, 217, 239);"> </span><span class="kwd" style="color: rgb(249, 38, 89);">if</span><span class="pln" style="color: rgb(102, 217, 239);"> </span><span class="pun" style="color: rgb(248, 248, 242);">(</span><span class="pln" style="color: rgb(102, 217, 239);">m_pReference</span><span class="pun" style="color: rgb(248, 248, 242);">-></span><span class="typ" style="color: rgb(166, 226, 46);">Release</span><span class="pun" style="color: rgb(248, 248, 242);">()</span><span class="pln" style="color: rgb(102, 217, 239);"> </span><span class="pun" style="color: rgb(248, 248, 242);">==</span><span class="pln" style="color: rgb(102, 217, 239);"> </span><span class="lit" style="color: rgb(174, 129, 255);">0</span><span class="pun" style="color: rgb(248, 248, 242);">)</span><span class="pln" style="color: rgb(102, 217, 239);"> </span><span class="pun" style="color: rgb(248, 248, 242);">{</span><span class="pln" style="color: rgb(102, 217, 239);"> SAFE_DELETE</span><span class="pun" style="color: rgb(248, 248, 242);">(</span><span class="pln" style="color: rgb(102, 217, 239);">m_pData</span><span class="pun" style="color: rgb(248, 248, 242);">);</span><span class="pln" style="color: rgb(102, 217, 239);"> SAFE_DELETE</span><span class="pun" style="color: rgb(248, 248, 242);">(</span><span class="pln" style="color: rgb(102, 217, 239);">m_pReference</span><span class="pun" style="color: rgb(248, 248, 242);">);</span><span class="pln" style="color: rgb(102, 217, 239);"> </span><span class="pun" style="color: rgb(248, 248, 242);">}</span><span class="pln" style="color: rgb(102, 217, 239);"> m_pData </span><span class="pun" style="color: rgb(248, 248, 242);">=</span><span class="pln" style="color: rgb(102, 217, 239);"> pObject</span><span class="pun" style="color: rgb(248, 248, 242);">;</span><span class="pln" style="color: rgb(102, 217, 239);"> m_pReference </span><span class="pun" style="color: rgb(248, 248, 242);">=</span><span class="pln" style="color: rgb(102, 217, 239);"> </span><span class="kwd" style="color: rgb(249, 38, 89);">new</span><span class="pln" style="color: rgb(102, 217, 239);"> </span><span class="typ" style="color: rgb(166, 226, 46);">KRefCount</span><span class="pun" style="color: rgb(248, 248, 242);">;</span><span class="pln" style="color: rgb(102, 217, 239);"> m_pReference</span><span class="pun" style="color: rgb(248, 248, 242);">-></span><span class="typ" style="color: rgb(166, 226, 46);">AddRef</span><span class="pun" style="color: rgb(248, 248, 242);">();</span><span class="pln" style="color: rgb(102, 217, 239);"> </span><span class="pun" style="color: rgb(248, 248, 242);">}</span><span class="pln" style="color: rgb(102, 217, 239);"> T</span><span class="pun" style="color: rgb(248, 248, 242);">*</span><span class="pln" style="color: rgb(102, 217, 239);"> </span><span class="typ" style="color: rgb(166, 226, 46);">Detach</span><span class="pun" style="color: rgb(248, 248, 242);">()</span><span class="pln" style="color: rgb(102, 217, 239);"> </span><span class="pun" style="color: rgb(248, 248, 242);">{</span><span class="pln" style="color: rgb(102, 217, 239);"> T</span><span class="pun" style="color: rgb(248, 248, 242);">*</span><span class="pln" style="color: rgb(102, 217, 239);"> ptr </span><span class="pun" style="color: rgb(248, 248, 242);">=</span><span class="pln" style="color: rgb(102, 217, 239);"> NULL</span><span class="pun" style="color: rgb(248, 248, 242);">;</span><span class="pln" style="color: rgb(102, 217, 239);"> </span><span class="kwd" style="color: rgb(249, 38, 89);">if</span><span class="pln" style="color: rgb(102, 217, 239);"> </span><span class="pun" style="color: rgb(248, 248, 242);">(</span><span class="pln" style="color: rgb(102, 217, 239);">m_pData</span><span class="pun" style="color: rgb(248, 248, 242);">)</span><span class="pln" style="color: rgb(102, 217, 239);"> </span><span class="pun" style="color: rgb(248, 248, 242);">{</span><span class="pln" style="color: rgb(102, 217, 239);"> ptr </span><span class="pun" style="color: rgb(248, 248, 242);">=</span><span class="pln" style="color: rgb(102, 217, 239);"> m_pData</span><span class="pun" style="color: rgb(248, 248, 242);">;</span><span class="pln" style="color: rgb(102, 217, 239);"> m_pData </span><span class="pun" style="color: rgb(248, 248, 242);">=</span><span class="pln" style="color: rgb(102, 217, 239);"> NULL</span><span class="pun" style="color: rgb(248, 248, 242);">;</span><span class="pln" style="color: rgb(102, 217, 239);"> m_pReference</span><span class="pun" style="color: rgb(248, 248, 242);">-></span><span class="typ" style="color: rgb(166, 226, 46);">Reset</span><span class="pun" style="color: rgb(248, 248, 242);">();//</span><span class="pun"><span style="color:#ffff00;">不理解這麼地方,這個地方對應的Attach應該是m_pReference->Release();不應該是reset.???</span></span><span class="pln" style="color: rgb(102, 217, 239);"> </span><span class="pun" style="color: rgb(248, 248, 242);">}</span><span class="pln" style="color: rgb(102, 217, 239);"> </span><span class="kwd" style="color: rgb(249, 38, 89);">return</span><span class="pln" style="color: rgb(102, 217, 239);"> ptr</span><span class="pun" style="color: rgb(248, 248, 242);">;//</span><span class="pun"><span style="color:#ffff00;">將m_pData置null.將資料通過ptr返回</span></span><span class="pln" style="color: rgb(102, 217, 239);"> </span><span class="pun" style="color: rgb(248, 248, 242);">}</span><span class="pln" style="color: rgb(102, 217, 239);"> </span><span class="kwd" style="color: rgb(249, 38, 89);">private</span><span class="pun" style="color: rgb(248, 248, 242);">:</span><span class="pln" style="color: rgb(102, 217, 239);"> </span><span class="typ" style="color: rgb(166, 226, 46);">KRefCount</span><span class="pun" style="color: rgb(248, 248, 242);">*</span><span class="pln" style="color: rgb(102, 217, 239);"> m_pReference</span><span class="pun" style="color: rgb(248, 248, 242);">;</span><span class="pln" style="color: rgb(102, 217, 239);"> T</span><span class="pun" style="color: rgb(248, 248, 242);">*</span><span class="pln" style="color: rgb(102, 217, 239);"> m_pData</span><span class="pun" style="color: rgb(248, 248, 242);">;</span><span class="pln" style="color: rgb(102, 217, 239);"> </span><span class="pun" style="color: rgb(248, 248, 242);">};</span><span class="pln" style="color: rgb(102, 217, 239);"> </span><span class="kwd" style="color: rgb(249, 38, 89);">class</span><span class="pln" style="color: rgb(102, 217, 239);"> </span><span class="typ" style="color: rgb(166, 226, 46);">CTest</span><span class="pln" style="color: rgb(102, 217, 239);"> </span><span class="pun" style="color: rgb(248, 248, 242);">{</span><span class="pln" style="color: rgb(102, 217, 239);"> </span><span class="kwd" style="color: rgb(249, 38, 89);">public</span><span class="pun" style="color: rgb(248, 248, 242);">:</span><span class="pln" style="color: rgb(102, 217, 239);"> </span><span class="typ" style="color: rgb(166, 226, 46);">CTest</span><span class="pun" style="color: rgb(248, 248, 242);">(</span><span class="kwd" style="color: rgb(249, 38, 89);">int</span><span class="pln" style="color: rgb(102, 217, 239);"> b</span><span class="pun" style="color: rgb(248, 248, 242);">)</span><span class="pln" style="color: rgb(102, 217, 239);"> </span><span class="pun" style="color: rgb(248, 248, 242);">:</span><span class="pln" style="color: rgb(102, 217, 239);"> a</span><span class="pun" style="color: rgb(248, 248, 242);">(</span><span class="pln" style="color: rgb(102, 217, 239);">b</span><span class="pun" style="color: rgb(248, 248, 242);">)</span><span class="pln" style="color: rgb(102, 217, 239);"> </span><span class="pun" style="color: rgb(248, 248, 242);">{}</span><span class="pln" style="color: rgb(102, 217, 239);"> </span><span class="kwd" style="color: rgb(249, 38, 89);">private</span><span class="pun" style="color: rgb(248, 248, 242);">:</span><span class="pln" style="color: rgb(102, 217, 239);"> </span><span class="kwd" style="color: rgb(249, 38, 89);">int</span><span class="pln" style="color: rgb(102, 217, 239);"> a</span><span class="pun" style="color: rgb(248, 248, 242);">;</span><span class="pln" style="color: rgb(102, 217, 239);"> </span><span class="pun" style="color: rgb(248, 248, 242);">};</span><span class="pln" style="color: rgb(102, 217, 239);"> </span><span class="kwd" style="color: rgb(249, 38, 89);">int</span><span class="pln" style="color: rgb(102, 217, 239);"> main</span><span class="pun" style="color: rgb(248, 248, 242);">()</span><span class="pln" style="color: rgb(102, 217, 239);"> </span><span class="pun" style="color: rgb(248, 248, 242);">{//</span><span class="pun"><span style="color:#ffff00;">模板類,帶引數,T就是CTest</span></span><span class="pln" style="color: rgb(102, 217, 239);"> </span><span class="typ" style="color: rgb(166, 226, 46);">SmartPtr</span><span class="pun" style="color: rgb(248, 248, 242);"><</span><span class="typ" style="color: rgb(166, 226, 46);">CTest</span><span class="pun" style="color: rgb(248, 248, 242);">></span><span class="pln" style="color: rgb(102, 217, 239);"> pSmartPtr1</span><span class="pun" style="color: rgb(248, 248, 242);">(</span><span class="kwd" style="color: rgb(249, 38, 89);">new</span><span class="pln" style="color: rgb(102, 217, 239);"> </span><span class="typ" style="color: rgb(166, 226, 46);">CTest</span><span class="pun" style="color: rgb(248, 248, 242);">(</span><span class="lit" style="color: rgb(174, 129, 255);">10</span><span class="pun" style="color: rgb(248, 248, 242);">));</span><span class="pln" style="color: rgb(102, 217, 239);"> </span><span class="typ" style="color: rgb(166, 226, 46);">SmartPtr</span><span class="pun" style="color: rgb(248, 248, 242);"><</span><span class="typ" style="color: rgb(166, 226, 46);">CTest</span><span class="pun" style="color: rgb(248, 248, 242);">></span><span class="pln" style="color: rgb(102, 217, 239);"> pSmartPtr2</span><span class="pun" style="color: rgb(248, 248, 242);">(</span><span class="kwd" style="color: rgb(249, 38, 89);">new</span><span class="pln" style="color: rgb(102, 217, 239);"> </span><span class="typ" style="color: rgb(166, 226, 46);">CTest</span><span class="pun" style="color: rgb(248, 248, 242);">(</span><span class="lit" style="color: rgb(174, 129, 255);">20</span><span class="pun" style="color: rgb(248, 248, 242);">));</span><span class="pln" style="color: rgb(102, 217, 239);"> pSmartPtr1 </span><span class="pun" style="color: rgb(248, 248, 242);">=</span><span class="pln" style="color: rgb(102, 217, 239);"> pSmartPtr2</span><span class="pun" style="color: rgb(248, 248, 242);">;</span><span class="pln" style="color: rgb(102, 217, 239);"> </span><span class="pun" style="color: rgb(248, 248, 242);">}</span></code>
相關文章
- 智慧指標2024-08-29指標
- [CPP] 智慧指標2021-01-18指標
- 什麼是智慧指標?為什麼要用智慧指標?2019-03-29指標
- openfoam 智慧指標探索2023-03-02指標
- vtk智慧指標2016-04-30指標
- 智慧指標學習2013-05-31指標
- 【c++】智慧指標2015-06-05C++指標
- C++智慧指標2024-06-16C++指標
- 智慧指標之手撕共享指標shared_ptr2024-09-22指標
- UE4 智慧指標2020-10-27指標
- C++11 智慧指標2017-12-29C++指標
- 「C++」理解智慧指標2013-07-19C++指標
- auto_ptr 智慧指標2009-01-08指標
- SMART POINTER(智慧指標) (轉)2007-09-30指標
- 智慧指標用法學習2024-05-21指標
- 指標儲存地址分析2018-04-08指標
- 財務分析常用指標2006-06-30指標
- 遊戲分析指標之--LTV2016-03-24遊戲指標
- 指標+AI:邁向智慧化,讓指標應用更高效2024-04-24指標AI
- C++ 用智慧指標這樣包裝 this 指標是否可行2024-07-16C++指標
- C++進階(智慧指標)2023-01-03C++指標
- C++11智慧指標用法2020-10-05C++指標
- C++ 智慧指標詳解2016-10-18C++指標
- [CareerCup] 13.8 Smart Pointer 智慧指標2015-11-02指標
- 程式分析與優化 - 5 指標分析2022-05-29優化指標
- 資料分析——指標理論2021-12-08指標
- 財務分析指標體系2010-04-14指標
- 遊戲分析指標之--DAU/MAU2016-03-24遊戲指標
- 遊戲分析中的重要指標2016-03-24遊戲指標
- 詳解成熟的資料標註工具—智慧標註平臺2023-01-09
- NULL 指標、零指標、野指標2016-10-19Null指標
- C++標準庫有四種智慧指標2020-12-15C++指標
- C++筆記(11) 智慧指標2021-08-28C++筆記指標
- 話說智慧指標發展之路2016-12-16指標
- c++ 智慧指標用法詳解2018-06-06C++指標
- C++智慧指標簡單剖析2014-11-29C++指標
- c++ auto_ptr 智慧指標2013-08-08C++指標
- 【C++智慧指標 auto_ptr】2015-08-08C++指標