普通new和placement new的重载
对于自定义对象,我们可以重载普通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的重载的更多相关文章
- new 、operator new 和 placement new
一.原生operator new 我们先从原生operator new开始.考虑如下代码,它用来分配5个int型的空间并返回指向他们的指针[1]: int* v = static_cast<in ...
- 【转】placement new
原文:http://www.cnblogs.com/wanghetao/archive/2011/11/21/2257403.html 1. placement new的含义placement new ...
- [C++空间分配]new运算符、operator new、placement new的区别于联系
先科普一下: 1. new的执行过程: (1)通过operator new申请内存 (2)使用placement new调用构造函数(内置类型忽略此步) (3)返回内存指针 2. new和malloc ...
- C++ Placement New
先看一个题目: #include <stdio.h> #include <iostream> using namespace std; struct Base { int j; ...
- 读书笔记 effctive c++ Item 52 如果你实现了placement new,你也要实现placement delete
1. 调用普通版本的operator new抛出异常会发生什么? Placement new和placement delete不是C++动物园中最常遇到的猛兽,所以你不用担心你对它们不熟悉.当你像下面 ...
- 小结:c++中的new、operator new和placement new
小结:c++中的new.operator new和placement new new(也称作new operator),是new 操作符,不可重载 class T{...}; T *t = new T ...
- placement new (转)
原文出自:http://www.cnblogs.com/wanghetao/archive/2011/11/21/2257403.html 1. placement new的含义placement n ...
- C++中的new、operator new与placement new
转:http://www.cnblogs.com/luxiaoxun/archive/2012/08/10/2631812.html new/delete与operator new/operator ...
- 读书笔记 effective c++ Item 52 如果你实现了placement new,你也要实现placement delete
1. 调用普通版本的operator new抛出异常会发生什么? Placement new和placement delete不是C++动物园中最常遇到的猛兽,所以你不用担心你对它们不熟悉.当你像下面 ...
随机推荐
- SpringBoot 使用Thymeleaf解决静态页面跳转问题
参考:springboot配置跳转html页面 1,首先在pom文件中引入模板引擎jar包 <dependency> <groupId>org.springframework. ...
- 二十、MVC的WEB框架(Spring MVC)
一.Spring MVC 运行原理:客户端请求提交到DispatcherServlet,由DispatcherServlet控制器查询HandlerMapping,找到并分发到指定的Controlle ...
- 网页定位点击事件js响应函数教程(Chrome)
一.背景说明 在前端页面调试或者渗透测试(尤其是XSS)时,我们经常想定位js函数位置:比如点击了某个位置弹出了一个对话框,这是哪个文件的哪个js函数在响应. 本文以Chrome浏览器定位点击事件响应 ...
- oracle和sql server中,取前10条数据语法的区别
在sql server中,取数据中前10条语句,我们可以用top 10 这样语句,但是oracle就没有这个函数,接下来介绍它们之间的区别 1.sql server 取前10语句和随机10条的语法 - ...
- python 列表切片
列表切片是python语言独有的特征,大大方便了我们的编码. 首先,在介绍切片之前,必须要知道一个知识,就是python列表的读写,下标可以使用负数. insert,get,set 操作均可传入负数下 ...
- C++解析四-友员函数、内联函数、静态成员
友元函数 类的友元函数是定义在类外部,但有权访问类的所有私有(private)成员和保护(protected)成员.尽管友元函数的原型有在类的定义中出现过,但是友元函数并不是成员函数.友元可以是一个函 ...
- PHP开发者的Linux学习之路
谈起一个高效动态网站的构建,那就不得不提到LAMP,即Linux操作系统.Apache网络服务器.Mysql数据库.Perl.PHP或Python编程语言等开源产品所组成的网站架构框架,其最大的优势是 ...
- bzoj3976
题解: 先跑一下Sa 然后再用kmp匹配一下哪一些位置不行 然后二分答案 代码: #include<bits/stdc++.h> ; using namespace std; int t[ ...
- linux:ssh远程调用tomcat脚本时候出错
我们都知道,使用ssh在另一台机子执行一个ssh文件的语句是酱紫的 ssh root@1.9.7.56 "chmod 777 /opt/script/tomcatStop.sh ; sh / ...
- nginx:负载均衡实战(四)nginx+keepalived配置双机热备
1.下载安装 下载keepalived地址:http://www.keepalived.org/download.html 解压安装: tar -zxvf keepalived-.tar.gz 安装o ...