I know I can do this in C++:
string s[] = {"hi", "there"};
But is there anyway to delcare an array this way without delcaring string s[]?
e.g.
void foo(string[] strArray){
// some code
}
string s[] = {"hi", "there"}; // Works
foo(s); // Works
foo(new string[]{"hi", "there"}); // Doesn't work
In C++11 you can. A note beforehand: Don’t
newthe array, there’s no need for that.First,
string[] strArrayis a syntax error, that should either bestring* strArrayorstring strArray[]. And I assume that it’s just for the sake of the example that you don’t pass any size parameter.Note that it would be better if you didn’t need to pass the array size as an extra parameter, and thankfully there is a way: Templates!
Note that this will only match stack arrays, like
int x[5] = .... Or temporary ones, created by the use ofaliasabove.