下面的句子在c ++中的含义是什么 [英] What is the meaning of the below sentence in c++

查看:115
本文介绍了下面的句子在c ++中的含义是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

C ++的展示位置新

代表,它是打字的方式吗?或

in the below code what does Line 3 represents, is it the way of typecasting? or what

 void someCode()
 {
   char memory[sizeof(Fred)];     // Line #1
   void* place = memory;          // Line #2

   Fred* f = new(place) Fred();   // Line #3
   // The pointers f and place will be equal

   ...
 }


推荐答案

这是 Placement new

它允许您分配内存,然后在该特定内存中构造对象位置。

This is a typical usage of Placement new.
It allows you to allocate memory and then construct objects at that particular memory location.

第3行基本上只是调用构造函数 Fred :: Fred()构建函数中的指针将等于 place 。因此返回的指针 f 将等于 place

Line #3 essentially just calls the constructor Fred::Fred(). The this pointer in the Fred constructor will be equal to place. The returned pointer f will therefore be equal to place.

这篇关于下面的句子在c ++中的含义是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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