I am new to C++.
For a school project I need to make a function which will be able to return a string array.
Currently I have this in my header:
Config.h
string[] getVehicles(void);
Config.cpp
string[] Config::getVehicles(){
string test[5];
test[0] = "test0";
test[1] = "test1";
test[2] = "test2";
test[3] = "test3";
test[4] = "test4";
return test;}
Obviously this does not work but that’s the idea of what I am trying to do.
In Java this would be the way to do it. I’ve tried googling my problem but I didn’t come across any answers that were clear to be honest.
Maybe it is better to use a vector in this case, but this is not a correct answer for the question. The reason why it doesn’t work is that the variable test just exists in the scope of your function.
So you have to manage the memory on your own. Here is an example:
In this case you return a pointer of the position in the heap. All the memory in the heap has to free manually. So it is now your work to delete the memory, if you don’t need it anymore: