偶然在网上看到这么一段代码,想问下其中的new (pv)Solution()是什么用法
class Solution {
public:
int a;
char c;
static int sb;
};
Solution x;
void main()
{
Solution s;
void *pv = operator new(sizeof(Solution));
Solution *ps = new (pv)Solution(); //这是什么用法?
Solution *ps2 = new Solution();
}
关于c++的new运算的问题
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
3条回答 默认 最新
- fullyanfmotr 2015-12-18 12:02关注
谢谢各位回答,在c++primer找到答案了
new (place_address) type
new (place_address) type (initializers)
new (place_address) type [size]
new (place_address) type [size] { braced initializer list }
where place_address must be a pointer and the initializers provide (a possibly empty)
comma-separated list of initializers to use to construct the newly allocated object.
When called with an address and no other arguments, placement new uses
operator new(size_t, void*) to “allocate” its memory. This is the version of
operator new that we are not allowed to redefine (§ 19.1.1, p. 822). This function
does not allocate any memory; it simply returns its pointer argument. The overall new
expression then finishes its work by initializing an object at the given address. In
effect, placement new allows us to construct an object at a specific, preallocated
memory address.
效果就是在pv指向的内存上调用构造函数本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
- ¥50 我撰写的python爬虫爬不了 要爬的网址有反爬机制
- ¥15 Centos / PETSc / PETGEM
- ¥15 centos7.9 IPv6端口telnet和端口监控问题
- ¥120 计算机网络的新校区组网设计
- ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
- ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
- ¥20 海浪数据 南海地区海况数据,波浪数据
- ¥20 软件测试决策法疑问求解答
- ¥15 win11 23H2删除推荐的项目,支持注册表等