这可能吗?
我知道您可以使用列表语法初始化结构。
IE浏览器
struct Foo f = { a, b, c};
return f;
有可能像使用类和构造函数一样,在一行中做到这一点?
谢谢
最佳答案
如果要让结构保持POD,请使用创建它的函数:
Foo make_foo(int a, int b, int c) {
Foo f = { a, b, c };
return f;
}
Foo test() {
return make_foo(1, 2, 3);
}
使用C++ 0x时,uniform initialization消除了对该函数的需要:
Foo test() {
return Foo{1, 2, 3};
// or just:
return {1, 2, 3};
}
关于c++ - 在C++中一行初始化并返回一个结构,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/3080278/