批註:智慧指標分析

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>

相關文章