C++ new/delete 重載__C++

來源:互聯網
上載者:User
 首先,new和delete是運算子,重載new和delete是可能的。這樣做的原因是,有時希望使用某種特殊的動態記憶體分配方法。例如,可能有些分配子程式,他們的堆已耗盡,自動開始把一個磁碟檔案當虛儲存使用,或者使用者希望控制某一片儲存空間的分配等。

重載new和delete的格式如下:

void *operator new (size_t size){  .......//完成分配工作  return pointer_to_memory;}void operator delete(void *p){  ......//釋放由p指向的儲存空間}
1.局部重載new和delete(可以使用成員函數和友元函數兩種方式重載)
      使用new分配某個重載了new的累的對象空間時,先調用new的重載函數,再調用該類的建構函式,如果該類的建構函式有參數要求,則必須給出對應的實參。
      使用了delete釋放某個重載了delete的累的對象空間時,先調用類的解構函式,然後再調用重載的delete函數。
#include <iostream>#include <stdlib.h>#include <string.h>using namespace std;class three_d{private:    int x,y,z;public:    three_d(int a,int b,int c);    ~three_d()    {        cout << "Destructing\n";    }    void *operator new(size_t size);    void operator delete(void *p);    friend ostream & operator <<(ostream &stream,three_d obj);};three_d::three_d(int a,int b,int c){    cout << "Constructing\n";    x = a;    y = b;    z = c;}void *three_d::operator new(size_t size){    cout << "in threee_d new\n";    return malloc(size);}void three_d::operator delete(void *p){    cout << "in three_d delete\n" ;    free(p);}ostream &operator <<(ostream &os,three_d obj){    os << obj.x << ",";    os << obj.y << ",";    os << obj.z << "\n";    return os;}int main(int argc,char *argv[]){    three_d *p = new three_d(1,2,3);    three_d *p1 = new three_d(4,5,6);    if(!p || !p1)    {        cout << "Allocation failure" << endl;        return 1;    }    cout << *p << *p1;    delete p;    delete p1;    int *pnum;    pnum = new int;    *pnum = 0;    cout << "num = " << *pnum << endl;    delete pnum;    cout << "Application Run Successfully!" << endl;    return 0;}
2.全域重載new和delete
      可以在任何類說明之外重在new和delete,使它們成為全域的。當new和delete被重載為全域時,C++原來的new與delete被忽略,並且重載的運算子用於所有類型(包括標準型和使用者定義型別)的分配要求。
#include <iostream>#include <stdlib.h>#include <string.h>using namespace std;class three_d{private:    int x,y,z;public:    three_d(int a,int b,int c);    ~three_d()    {        cout << "Destructing\n";    }    friend ostream & operator <<(ostream &stream,three_d obj);};three_d::three_d(int a,int b,int c){    cout << "Constructing\n";    x = a;    y = b;    z = c;}void *operator new(size_t size){    cout << "in threee_d new\n";    return malloc(size);}void operator delete(void *p){    cout << "in three_d delete\n" ;    free(p);}ostream &operator <<(ostream &os,three_d obj){    os << obj.x << ",";    os << obj.y << ",";    os << obj.z << "\n";    return os;}int main(int argc,char *argv[]){    three_d *p = new three_d(1,2,3);    three_d *p1 = new three_d(4,5,6);    if(!p || !p1)    {        cout << "Allocation failure" << endl;        return 1;    }    cout << *p << *p1;    delete p;    delete p1;    int *pnum;    pnum = new int;    *pnum = 0;    cout << "num = " << *pnum << endl;    delete pnum;    cout << "Application Run Successfully!" << endl;    return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.