深入new/delete:Operator new的全局重載

Operator new 的全局重載

原文地址:http://blog.csdn.net/zhenjing/article/details/4354880

我們經常看到這么一句話: operator new 可以重載, placement new 不可重載。其實此處所說的不可重載應該是指全局的 placement new 不可重載,對于類域中的 placement new 是可以重載的,而且只要重載了任何一種形式的 operator new 都應該順便重載 placement new , 即 void * operator new(std::size_t count, void *ptr) 。

操作符重載一般用于特定類型,名字解析過程同一般的函數重載。 Operator new 由于其特殊性,編譯器提供了默認提供 6 種全局重載形式,同時還允許用戶提供自定義的全局 operator new ,其參數甚至可以和全局版本一樣,除全局 placement new 外。對于類域,任何形式的 new 都是可以重載的,包括 placement new 形式。

?

全局的 operator new( 函數 ) 有六種重載形式

void *operator new(std::size_t count)

??? throw(std::bad_alloc);?????????? // 一般的版本

?

void *operator new(std::size_t count,?? // 兼容早版本的 new

??? const std::nothrow_t&) throw();?? // 內存分配失敗不會拋出異常

?

void *operator new(std::size_t count, void *ptr) throw();? //placement 版本

??????????????????????????????????????

void *operator new[](std::size_t count)? //

??? throw(std::bad_alloc);

?

void *operator new[](std::size_t count,? //

??? const std::nothrow_t&) throw();

?

void *operator new[](std::size_t count, void *ptr) throw();

?

重載 operator new 規則

重載 operator new 的參數個數是可以任意的 , 只需要保證第一個參數為 size_t, 返回類型為 void * 即可 , 而且其重載的參數類型也不必包含自定義類型 . 更一般的說 , operator new 的重載更像是一個函數的重載 , 而不是一個操作符的重載 . 如:

?

全局重載示例:

void* operator new(size_t size)? // 重載成功

{

?? printf("global new/n");

?? return malloc(size);

?? //return ::operator new(size);? // 遞歸調用提示 (warning)

}

?

//void *operator new(std::size_t size, void *ptr) // 無法重載

//{

//???? printf("global new/n");

//???? return ::operator new(size,ptr);

//}

?

void * operator new(size_t size, const std::nothrow_t& e) // 重載成功 , 遞歸調用提示 (warning)

{

?????? printf("global new/n");

?????? return ::operator new(size, e);

}

?

一般形式的 operator new 重載示例:

void * operator new(size_t size, int x, int y, int z)

{

??? ...

}

X * pX = new (1, 2, 3) X;

?

char data[1000][sizeof(foo)];

inline void* operator new(size_t size, int n)

{

??????? return data[n];

}

就可以使用這樣有趣的語法來創建對象 :

foo *p=new(6) foo(); // 把對象創建在 data 的第六個單元上

轉載于:https://www.cnblogs.com/dragon2012/p/3571838.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/377286.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/377286.shtml
英文地址,請注明出處:http://en.pswp.cn/news/377286.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

C++基礎知識點整理

基本語法 1、static關鍵字的作用 1、全局靜態變量 加了static關鍵字的全局變量只能在本文件中使用。 存儲在靜態存儲區,整個程序運行期間都存在。 2、局部靜態變量 作用域仍為局部作用域。 不過離開作用域之后,并沒有銷毀,而是貯存程序中&a…

Haskell學習筆記

《learn you a Haskell》這書的結構與常見的語言入門教材完全不一樣。事實上,即使學到第八章,你還是寫不出正常的程序…因為到現在為止還沒告訴你入口點模塊怎么寫,IO部分也留在了最后幾章才介紹。最重要的是,沒有系統的總結數據類…

組合問題 已知組合數_組合和問題

組合問題 已知組合數Description: 描述: This is a standard interview problem to make some combination of the numbers whose sum equals to a given number using backtracking. 這是一個標準的面試問題,它使用回溯功能將總和等于給定數字的數字進…

可變參數模板、右值引用帶來的移動語義完美轉發、lambda表達式的理解

可變參數模板 可變參數模板對參數進行了高度泛化&#xff0c;可以表示任意數目、任意類型的參數&#xff1a; 語法為&#xff1a;在class或者typename后面帶上省略號。 Template<class ... T> void func(T ... args) {// }T:模板參數包&#xff0c;args叫做函數參數包 …

BI-SqlServer

一.概述 SqlServer數據倉庫ETL組件 IntegrationServiceOLAP組件 AnalysisService報表 ReportingServiceMDX(查多維數據集用的)和DMX(查挖掘模型用的)。二.商業智能-Analysis Services 項目 構建挖掘模型1構建挖掘模型2構建挖掘模型3三.商業智能-SqlServerAnalysis-Asp.net WebS…

python 子圖大小_Python | 圖的大小

python 子圖大小In some cases, the automatic figure size generated by the matplotlib.pyplot is not visually good or there could be some non-acceptable ratio in the figure. So, rather than allowing a pyplot to decide the figure size, we can manually define t…

《設計模式整理》

目錄常見設計模式如何保證單例模式只有一個實例單例模式中的懶漢與餓漢模式OOP設計模式的五項原則單例模式中的懶漢加載&#xff0c;如果并發訪問該怎么做常見設計模式 單例模式&#xff1a; 單例模式主要解決了一個全局使用的類頻繁的創建和銷毀的問題。 單例模式下確保某一個…

JSON學習資料整理

1.什么是JSON JSON(JavaScript Object Notation) 是一種輕量級的數據交換格式。它基于JavaScript的一個子集。 JSON采用完全獨立于語言的文本格式&#xff0c;但是也使用了類似于C語言家族的習慣&#xff08;包括C, C, C#, Java, JavaScript, Perl, Python等&#xff09;。這些…

OSI七層模型及其數據的封裝和解封過程

OSI(Open System Interconnection)參考模型把網絡分為七層: 1.物理層(Physical Layer) 物理層主要傳輸原始的比特流,集線器(Hub)是本層的典型設備; 2.數據鏈路層(Data Link Layer) 數據鏈路層負責在兩個相鄰節點間無差錯的傳送以幀為單位的數據,本層的典型設備是交換機(Switch)…

rss聚合模式案例_RSS的完整形式是什么?

rss聚合模式案例RSS&#xff1a;真正簡單的聯合 (RSS: Really Simple Syndication) RSS is an abbreviation of Really Simple Syndication. It is also called Rich Site Summary. It is quality attainment for the syndication of collection of web content and used to di…

《MySQL——38道查詢練習(無連接查詢)》

目錄一、準備數據1、創建數據庫2、創建學生表3、創建教師表4、創建課程表5、創建成績表6、添加數據二、查詢練習1、查詢 student 表的所有行2、查詢 student 表中的 name、sex 和 class 字段的所有行3、查詢 teacher 表中不重復的 department 列4、查詢 score 表中成績在60-80之…

工作經常使用的SQL整理,實戰篇(一)

工作經常使用的SQL整理&#xff0c;實戰篇&#xff08;一&#xff09; 原文:工作經常使用的SQL整理&#xff0c;實戰篇&#xff08;一&#xff09;工作經常使用的SQL整理&#xff0c;實戰篇&#xff0c;地址一覽&#xff1a; 工作經常使用的SQL整理&#xff0c;實戰篇&#xff…

XPth和XSLT的一些簡單用法

&#xff08;目的在于讓大家知道有這個東西的存在&#xff09; XPath:即XML Path語言(Xpath)表達式使用路徑表示法(像在URL中使用一樣)來為XML文檔的各部分尋址&#xff01; 關于XPath如何使用了&#xff0c;我們來看看&#xff01;當然這里面的代碼只是入門&#xff0c;更深層…

isc dhcp_ISC的完整形式是什么?

isc dhcpISC&#xff1a;印度學校證書 (ISC: Indian School Certificate) ISC is an abbreviation of the Indian School Certificate. It alludes to the 12th class examination or higher secondary examination conducted by the Council for the Indian School Certificat…

《MySQL——連接查詢》

內連接&#xff1a; inner join 或者 join 外連接 1、左連接 left join 或 left outer join 2、右連接 right join 或 right outer join 3、完全外連接 full join 或 full outer join 圖示理解 全連接 創建person表和card表 CREATE DATABASE testJoin;CREATE TABLE person (…

win7下 apache2.2 +php5.4 環境搭建

這篇文章很好 沒法復制 把鏈接粘貼來http://www.360doc.com/content/13/0506/13/11495619_283349585.shtml# 現在能復制了&#xff1a; 把任何一篇你要復制、卻不讓復制的文章收藏入收藏夾(直接CtrlD,確定) 2在收藏夾中&#xff0c;右擊剛才收藏的那個網址&#xff0c;點屬性 3…

HDU_1533 Going Home(最優匹配) 解題報告

轉載請注明出自cxb:http://blog.csdn.net/cxb569262726 題目鏈接&#xff1a;http://acm.hdu.edu.cn/showproblem.php?pid1533 說實話&#xff0c;這個題目剛開始還真看不出是完備匹配下的最大權匹配&#xff08;當然&#xff0c;這個也可以用網絡流做。&#xff08;應該是添加…

c#中 uint_C#中的uint關鍵字

c#中 uintC&#xff03;uint關鍵字 (C# uint keyword) In C#, uint is a keyword which is used to declare a variable that can store an integral type of value (unsigned integer) the range of 0 to 4,294,967,295. uint keyword is an alias of System.UInt32. 在C&…

《MySQL——事務》

目錄事務的必要性MySQL中如何控制事務手動開啟事務事務的四大特征事務的四大特征事務開啟方式事務手動提交與手動回滾事務的隔離性臟讀現象不可重復讀現象幻讀現象串行化一些補充使用長事務的弊病commit work and chain的語法是做什么用的?怎么查詢各個表中的長事務&#xff1…

運行在TQ2440開發板上以及X86平臺上的linux內核編譯

一、運行在TQ2440開發板上的linux內核編譯 1、獲取源碼并解壓 直接使用天嵌移植好的“linux-2.6.30.4_20100531.tar.bz2”源碼包。 解壓&#xff08;天嵌默認解壓到/opt/EmbedSky/linux-2.6.30.4/中&#xff09; tar xvjf linux-2.6.30.4_20100531.tar.bz2 -C / 2、獲取默認配置…