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函式以釋放物件所用的記憶體。






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