gpt4 book ai didi

c++ - `new auto` 有什么作用?

转载 作者:IT老高 更新时间:2023-10-28 21:38:59 26 4
gpt4 key购买 nike

当我使用 new auto 是什么意思?考虑表达式:

new auto(5)

动态分配对象的类型是什么?它返回的指针是什么类型的?

最佳答案

在这种情况下,auto(5) 解析为 int(5)

您正在从堆中分配一个新的 int,初始化为 5

(所以,它返回一个 int *)

经许可引用 Andy Prowl 的足智多谋的回答:

根据 C++11 标准的第 5.3.4/2 段:

If the auto type-specifier appears in the type-specifier-seq of a new-type-id or type-id of a new-expression, the new-expression shall contain a new-initializer of the form

( assignment-expression )

The allocated type is deduced from the new-initializer as follows: Let e be the assignment-expression in the new-initializer and T be the new-type-id or type-id of the new-expression, then the allocated type is the type deduced for the variable x in the invented declaration (7.1.6.4):

T x(e);

[ Example:

new auto(1); // allocated type is int
auto x = new auto(’a’); // allocated type is char, x is of type char*

end example ]

关于c++ - `new auto` 有什么作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15935080/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com