I'm wondering, is it possible to pass an array to a function directly using C++0x? this what they call initialiser list. I don't get the idea. I'm looking for something like:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
double sumFunc(double[] list)
{
i = 0;
double sum = 0;
while(list[i] != '\0')
{
sum += list[i];
}
return sum;
}
int main()
{
cout << sumFunc({1,2,3,4,5}) << endl;
}
So the idea is, I want to pass the array directly without initialising it before the call. Is this possible with this way or any other way?
I don't know much about this particular rule in c++0x, but I don't find it hard to understand. C++ standard makers are trying to make C++ more flexible, syntax-wise, so that special needs are met.
Let me ask you a question:
Which line of code is the first line of sunFunc()?
The answer is "i=0". It's not "double sumFunc(double[] list)". Function name is just a function pointer. The actual first line of code is "i=0".
Okay, now, in your word "I want to pass the array directly without initialising it before the call".
That means, you want the array to be initialized before "i=0" is executed.
Will the code do what you want?
Yes, because the array is initialized when the execution falls on "cout << sumFunc({1,2,3,4,5}) << endl;". It's apparently one step before "i=0" is executed.
Watch that while loop on Line 5, you may not have pasted the code but I don't see where that condition is ever met. Also have you done any work with the operator "new"? http://www.cplusplus.com/reference/std/new/