Is there a way to write the following function code
int* returnFilledArray() {
int* arr = new int[2];
arr[0] = 1;
arr[1] = 2;
return arr;
}
somehow like that? (create and fill the array as one liner).
int* returnFilledArray() {
return new int {1,2};
}
if have tried various combinations, but I allways get some syntax errors, so when if it's possible I would be thankful for a detailed explanation.
std::vectorinstead of a plain array.std::vectoralso has the advantage that you could use Boost.Assign (only needed pre-C++11).int[] returnFilledArray();andint* returnFilledArray()are exactly the same function signature, so that comment (while upvoted) makes no sense at all.