Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Array Initialization in Function Call or Constructor Call

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?

like image 471
david Avatar asked Apr 20 '10 16:04

david


1 Answers

Not in the current standard. It will be possible in C++11

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 } );
}
like image 143
David Rodríguez - dribeas Avatar answered Sep 22 '22 13:09

David Rodríguez - dribeas