这个new()语句的说明 [英] Explanation of this new() statement

查看:148
本文介绍了这个new()语句的说明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在查看一段C ++代码,我来到这个语句块:

I am reviewing a section of C++ code an I came accross this statement block:

static void Vector3DefaultConstructor(Vector3 *self)
{    
    new(self) Vector3();    
}



我之前没有用这种方式使用新的操作符。

I have not come accross the new operator being used in this way before. Can someone explain why new is being called in this way?

推荐答案

这被称为placement new 。默认情况下,它不分配内存,而是在给定位置(这里, self )构造对象。但

This is called "placement new". By default, it does not allocate memory but instead constructs the object at the given location (here, self). It can, however, be overloaded for a class.

请参阅 FAQ 了解更多信息。

正确的销毁使用放置 new 是通过直接调用析构函数:

The correct way to destroy an object constructed using placement new is by calling the destructor directly:

obj->~Vector3();

这篇关于这个new()语句的说明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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