Can you return in c++ a two dimensional array without using vectors or pointers?
I found this answer but I don't want to manage the ram myself when using pointsers https://stackoverflow.com/a/4172103/4954387
Can you return in c++ a two dimensional array without using vectors or pointers?
I found this answer but I don't want to manage the ram myself when using pointsers https://stackoverflow.com/a/4172103/4954387
Can you return in c++ a two dimensional array without using vectors or pointers?
Only if the size is known at compile time and only if you wrap the array in a class type. Just like one-dimensional arrays. You cannot return arrays directly.
struct Array
{
int array[2][3];
};
Array f()
{
return Array { 1, 1, 1, 2, 2, 2 };
}
int main()
{
auto const array = f();
}
If you consider using a class like Array
, think twice and mind that the standard library already offers std::array
for that.
If you don't know the size at compile time, drop the "without using vectors" requirement and use std::vector
.