对于自定义对象,我们可以重载普通new操作符,这时候使用new Test()时就会调用到我们重载的普通new操作符。

示例程序:

 #include <iostream>
#include <cstdlib> using namespace std; class Test
{
public:
Test()
{
cout << "Test()" << endl;
} void* operator new(unsigned int size)
{
void* ret = malloc(sizeof(int) * size); cout << "normal new" << endl; return ret;
} }; int main()
{
Test* t = new Test(); Test t2; return ;
}

执行结果如下:

调用placement new,程序如下:

 #include <iostream>
#include <cstdlib> using namespace std; class Test
{
public:
Test()
{
cout << "Test()" << endl;
} void* operator new(unsigned int size)
{
void* ret = malloc(sizeof(int) * size); cout << "normal new" << endl; return ret;
} }; int main()
{
Test* t = new Test(); Test* t1 = new((void*)t)Test(); Test t2; return ;
}

编译结果如下:

提示我们没有对应的函数,也就是placement new没有重载。

更改程序:

 #include <iostream>
#include <cstdlib> using namespace std; class Test
{
public:
Test()
{
cout << "Test()" << endl;
} void* operator new(unsigned int size)
{
void* ret = malloc(sizeof(int) * size); cout << "normal new" << endl; return ret;
} void* operator new(unsigned int size, void* loc)
{
cout << "placement new" << endl;
return loc;
} }; int main()
{
Test* t = new Test(); Test* t1 = new((void*)t)Test(); Test t2; return ;
}

结果如下:

再次给出一个测试程序:

 #include <iostream>
#include <cstdlib> using namespace std; class Test
{
public:
Test()
{
cout << "Test()" << endl;
} void* operator new(unsigned int size)
{
void* ret = malloc(sizeof(int) * size); cout << "normal new" << endl; return ret;
} void* operator new(unsigned int size, void* loc)
{
cout << "placement new" << endl;
return loc;
} }; int main()
{
Test* t = new Test(); Test* t1 = new((void*)t)Test(); Test t2; Test* t3 = new((void*)&t2)Test(); int a = ; int* p = new((void*)&a)int(); cout << "a = " << a << endl; return ;
}

运行结果如下:

可以看到普通内置类型可以直接使用placement new。

普通new和placement new的重载的更多相关文章

  1. new 、operator new 和 placement new

    一.原生operator new 我们先从原生operator new开始.考虑如下代码,它用来分配5个int型的空间并返回指向他们的指针[1]: int* v = static_cast<in ...

  2. 【转】placement new

    原文:http://www.cnblogs.com/wanghetao/archive/2011/11/21/2257403.html 1. placement new的含义placement new ...

  3. [C++空间分配]new运算符、operator new、placement new的区别于联系

    先科普一下: 1. new的执行过程: (1)通过operator new申请内存 (2)使用placement new调用构造函数(内置类型忽略此步) (3)返回内存指针 2. new和malloc ...

  4. C++ Placement New

    先看一个题目: #include <stdio.h> #include <iostream> using namespace std; struct Base { int j; ...

  5. 读书笔记 effctive c++ Item 52 如果你实现了placement new,你也要实现placement delete

    1. 调用普通版本的operator new抛出异常会发生什么? Placement new和placement delete不是C++动物园中最常遇到的猛兽,所以你不用担心你对它们不熟悉.当你像下面 ...

  6. 小结:c++中的new、operator new和placement new

    小结:c++中的new.operator new和placement new new(也称作new operator),是new 操作符,不可重载 class T{...}; T *t = new T ...

  7. placement new (转)

    原文出自:http://www.cnblogs.com/wanghetao/archive/2011/11/21/2257403.html 1. placement new的含义placement n ...

  8. C++中的new、operator new与placement new

    转:http://www.cnblogs.com/luxiaoxun/archive/2012/08/10/2631812.html new/delete与operator new/operator ...

  9. 读书笔记 effective c++ Item 52 如果你实现了placement new,你也要实现placement delete

    1. 调用普通版本的operator new抛出异常会发生什么? Placement new和placement delete不是C++动物园中最常遇到的猛兽,所以你不用担心你对它们不熟悉.当你像下面 ...

随机推荐

  1. Leetcode 1003. 检查替换后的词是否有效

    1003. 检查替换后的词是否有效  显示英文描述 我的提交返回竞赛   用户通过次数245 用户尝试次数273 通过次数249 提交次数500 题目难度Medium 给定有效字符串 "ab ...

  2. Spring注解之@validated的使用

    spring-boot中可以用@validated来校验数据,如果数据异常则会统一抛出异常,方便异常中心统一处理.比如,我们判断一个输入参数是否合法,可以用如下方式 一 基础使用 因为spring-b ...

  3. Django之WSGI 和MVC/MTV

    一.什么是WSGI? WEB框架的本质是一个socket服务端接收用户请求,加工数据返回给客户端(Django),但是Django没有自带socket需要使用 别人的 socket配合Django才能 ...

  4. daal4py 随机森林模型训练mnist并保存模型给C++ daal predict使用

    # daal4py Decision Forest Classification Training example Serialization import daal4py as d4p import ...

  5. VMware如何进入安全模式

    VMware进入安全模式和物理机一样:使光标处于在虚拟机中激活状态,启动系统时不停按F8即可. 安全模式--只加载必要的驱动和进程:在cmd可以看到部份命令不能执行或命令功能不能完全实现. 网络安全模 ...

  6. absolute 导致点击事件无效

    方案一: 添加层数 z-index 方案二: 背景的透明度为0 background-color:#000; filter:alpha(opacity=0); opacity:0;

  7. edram install

    Edraw安装   1● 下载Edraw     2● 安装步骤 断网        

  8. python 利用turtle库绘制五角星

    # -*- coding: utf-8 –*-import turtleimport math def draw_polygon(aTurtle, size=50, n=3): for i in ra ...

  9. 1-2Controller之Session

    laravel5.5版本. 视频教程是慕课网中的:轻松学会Laravel-表单篇 1-2 /*session简介: 1.由于HTTP协议是无状态(Stateless)的,所以session提供一种保存 ...

  10. window有哪些属性?

    self:self代表自己,相当于window. parent:返回父窗口. top:返回顶层窗口,和parent作用一样. opener:窗口开启者. status:设置窗口状态栏的文本.