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 的第六个单元上

深入new/delete:Operator new的全局重载的更多相关文章

  1. C++ new operator, delete operator, operator new, operator delete, new placement

    http://www.younfor.com/cpp-new-placement-new-operator-new.html http://www.cnblogs.com/luxiaoxun/arch ...

  2. C++ delete operator做了什么事

    1.C++中的delete operator做了两件事:调用析构方法和调用operator delete释放内存. 2.考虑析构方法,如果析构方法是虚方法,调用指针真实类型的析构方法,否则调用表面类型 ...

  3. Cplus Overolad new and delete Operator

    思考:在C++类中,通过设计类的构造和析构函数,就已经把复杂的内存管理起来了. 及时是简单的结构体,也是有构造和析构函数的,而下面这种情况,可以在非结构中使用. /** Operator Overlo ...

  4. error C2665: “std::locale::facet::operator new”: 2 个重载中没有一个可以转换所有参数类型

    qt项目 qt creator项目由插件自动转换成的vs2015项目,为了发布少带些dll ,切换成vs2013项目,在更改了一些vs2013不支持的c++新标准写法之后,release可正常编过,但 ...

  5. 29.局部和全局重载new delete

    #include <iostream> #include <Windows.h> using namespace std; //全局内存管理,统计释放内存,分配内存 //重载全 ...

  6. cout 堆栈,operator&lt;&lt; 运算符重载输出问题

    在C++中cout的输出流其中,有一些问题非常easy出错,就比方以下这道简单程序.看似简单.但却是一个值得深思的问题~~ #include <iostream> using namesp ...

  7. new、operator new、placement new

    首先我们区分下几个容易混淆的关键词: new.operator new.placement new new和delete操作符我们应该都用过,它们是对堆中的内存进行申请和释放,而这两个都是不能被重载的 ...

  8. C++知识点 笔试常见

    C++知识点   一.#include “filename.h”和#include <filename.h>的区别 #include “filename.h”是指编译器将从当前工作目录上开 ...

  9. 重载operator new delete函数

    可以重载global的operator new delete 函数,细节如下: MyNewDelete.h #pragma once #include <stdlib.h> #includ ...

随机推荐

  1. 在向"带有自增字段的数据库表"中插入数据时,自定义"该自增字段"的数据

    在设计数据库表的时候,经常会使用自增主键或其他自增字段.比如: DB_UserGroups表中GroupID为该表主键,并为自增字段. 但在将某字段设置自增后,想在插入数据时,人为指定自增字段的数据内 ...

  2. php 读取功能分割大文件实例详解

    在php中,对于文件的读取时,最快捷的方式莫过于使用一些诸如file.file_get_contents之类的函数.但当所操作的文件是一个比较大的文件时,这些函数可能就显的力不从心, 下面将从一个需求 ...

  3. [gj]HK一行所见闻

    香港一行 20多年来,未未去过HK,前段时间由于工作关系去了趟HK.感触良多. 一清早,福田过关,做火车,做地铁,一通到了目的地. 总结对那边的印象: 1,所有人都是粤语,包括工作交流.而且他们不怎么 ...

  4. Android笔记一.深入理解Intent和IntentFilters(一)

    深入理解Intent和IntentFiler(一) 转载请表明出处:http://blog.csdn.net/u012637501(嵌入式_小J的天空)     为了比較深刻的理解并灵活使用Inten ...

  5. qt 例子地址

    http://blog.sina.com.cn/s/articlelist_2801495241_0_2.html qt打包http://blog.chinaunix.net/uid-24641004 ...

  6. u-boot中添加自定义命令

    1.u-boot命令机制u-boot中,每个命令都使用一个struct cmd_tbl_s结构体定义,该定义在include/command.h中实现:struct cmd_tbl_s{ char * ...

  7. 更改datatables的分页切换时的'processing'提示信息的式样

    jquery.dataTables.css .dataTables_wrapper .dataTables_processing { position: absolute; top: 50%; lef ...

  8. LeetCode: Permutations 解题报告

    Permutations Given a collection of numbers, return all possible permutations. For example,[1,2,3] ha ...

  9. LeetCode: Longest Valid Parentheses 解题报告

    Longest Valid Parentheses Given a string containing just the characters '(' and ')', find the length ...

  10. python pip 升级

      首先安装python,在百度中搜索python,进入python官网.点击download,选择电脑对应的系统进行下载,此处以windows系统的python 3.5.1进行介绍,点击即可下载. ...