zoukankan      html  css  js  c++  java
  • C++ new和delete重载

    C++ new和delete重载  

    2012-02-15 23:25:33|  分类: C/C++|举报|字号 订阅

     
     
          首先,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 ";
        }
        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 ";
        x = a;
        y = b;
        z = c;
    }

    void *three_d::operator new(size_t size)
    {
        cout << "in threee_d new ";
        return malloc(size);
    }

    void three_d::operator delete(void *p)
    {
        cout << "in three_d delete " ;
        free(p);
    }

    ostream &operator <<(ostream &os,three_d obj)
    {
        os << obj.x << ",";
        os << obj.y << ",";
        os << obj.z << " ";
        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 ";
        }
        friend ostream & operator <<(ostream &stream,three_d obj);
    };

    three_d::three_d(int a,int b,int c)
    {
        cout << "Constructing ";
        x = a;
        y = b;
        z = c;
    }

    void *operator new(size_t size)
    {
        cout << "in threee_d new ";
        return malloc(size);
    }

    void operator delete(void *p)
    {
        cout << "in three_d delete " ;
        free(p);
    }

    ostream &operator <<(ostream &os,three_d obj)
    {
        os << obj.x << ",";
        os << obj.y << ",";
        os << obj.z << " ";
        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;
    }
  • 相关阅读:
    CSS简单的四种引入方式
    html之表单标签
    html基础标签之head和body标签
    Python之协程的实现
    Python之实现不同版本线程池
    Python多进程之multiprocessing模块和进程池的实现
    Python之queue模块
    sqlserver 时间格式化
    关于sql server 代理(已禁用代理xp)解决办法
    sqlserver如何启动数据库邮件
  • 原文地址:https://www.cnblogs.com/jeanschen/p/3538830.html
Copyright © 2011-2022 走看看