new Expression versus operator new Function

C++ Primer 4/e在Optimizing Memory Allocation這個地方有一個術語:『

The library functions operator new and operator delete are misleadingly named. Unlike other operator functions, such as operator=, these functions do not overload the new or delete expressions. In fact, we cannot redefine the behavior of the new and delete expressions.

A new expression executes by calling an operator new function to obtain memory and then constructs an object in that memory. A delete expression executes by destroying an object and then calls an operator delete function to free the memory used by the object.

警告

Because the new (or delete) expressions and the underlying library functions have the same name, it is easy to confuse the two.』

中文版的這樣寫:『程式庫提供的operator new的函式和operator delete函式,其名稱很容易帶來混淆。不同於其他operator 函式(例如operator=),上述兩函式並沒有對new或delete算式進行重載。事實上我們不能重新定義new算式和delete算式的行為。

new算式的行為包括呼叫operator new函式取得記憶體,然後在該記憶體建構一個物件。delete算式的行為包括銷毀一個物件並呼叫operator delete函式以釋放物件所用的記憶體。

警告

由於new(或delete)算式及其底層所用函式的名稱相同,兩者很容易被混淆。』

感謝你看到這裡,很快就可以離開了,但最好的獎勵行動就是按一下幫我分享或留言,感恩喔~

點我分享到Facebook

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *