这是第二个新的? [英] What is this second new?

查看:175
本文介绍了这是第二个新的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是第二行? (回答另一个问题时出现。)

What is the second line? (Seen while answering another question.)

int * x = new int [1] ;
int * y = new (x) int;

第二行x和y具有相同的值(指向同一个地方)后。 y = x和第二行有什么区别?它是一个构造函数还是什么?

After the second line x and y have the same value (point to a same place). What's the difference between y = x and the second line? Is it like a constructor or something?

推荐答案

这是刊登位置新。它在 x 指向的内存中构造一个新的 int

It's placement new. It constructs a new int in the memory pointed to by x.

如果你尝试:

int * x = new int [1];
*x = 5;
std::cout << *x << std::endl;
int * y = new (x) int;
*y = 7;
std::cout << *x << std::endl;

输出将是:

5
7

这篇关于这是第二个新的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆