C ++中的函数调用或构造函数调用初始化数组 [英] C++ Array Initialization in Function Call or Constructor Call
问题描述
此问题是关系到后<一个href=\"http://stackoverflow.com/questions/2627223/c-template-class-constructor-with-variable-arguments\">here.是否有可能在初始化函数调用或构造函数调用数组?例如,Foo类的构造函数希望大小为3的数组,所以我想调用foo({0,0,0})。我已经试过这一点,这是行不通的。我希望能够初始化Foo类型对象的其他对象的构造函数初始化列表,或初始化Foo的不首先创建一个单独的数组。这可能吗?
This question is related to the post here. Is it possible to initialize an array in a function call or constructor call? For example, class foo's constructor wants an array of size 3, so I want to call foo( { 0, 0, 0 } ). I've tried this, and it does not work. I'd like to be able to initialize objects of type foo in other objects' constructor initialization lists, or initialize foo's without first creating a separate array. Is this possible?
推荐答案
不是在目前的标准。它会在C ++可能11
Not in the current standard. It will be possible in C++11
在GCC,您可以使用一个投来强制时间的创作,但它不是标准的C ++(C99):
In gcc you can use a cast to force the creation of a temporal, but it is not standard c++ (C99):
typedef int array[2];
void foo( array ) {} // Note: the actual signature is: void foo( int * )
int main() {
foo( (array){ 1, 2 } );
}
这篇关于C ++中的函数调用或构造函数调用初始化数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!