gpt4 book ai didi

c++ - int 的构造函数中参数的可变数量

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:27:00 24 4
gpt4 key购买 nike

我写了下面的代码片段,在 codepad.org

int main()
{
int *p = new int(5,6,7);
return 0;
}

我将 3 个参数传递给 int 的构造函数,同时为其动态分配内存。(根据我的说法,这不应该工作)。

但是当我在 visual studio 2010 编译器中执行相同的代码时,它正在使用最后一个参数编译和初始化值。为什么会这样?

最佳答案

VS2010 正在调用通用运算符并正确分配最后一个值。

http://en.wikipedia.org/wiki/Comma_operator

对于 gcc 试试这个

int main()
{
int *p = new int((5,6,7));
return 0;
}

关于c++ - int 的构造函数中参数的可变数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17770935/

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