i want to create a function(in a project) that returns an array. I m not quite sure about how can i do that.
int worker::*codebook(UnitType type){
int code[12];
if (type == UnitTypes::center){
int temp[12] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1};
code=temp;
}
return code;
}
where worker is the class and unitType an enumeration. I define the function in the header file as follows:
int *codebook(UnitType type);
My problems is the following:
cannot convert from 'int' to 'int Worker::*
Any idea about this??
The first problem in the code is the syntax. The signature should be:
Then, it is assigning to an array:
This is just not allowed by the language.
Finally, it’s returning a pointer to a local variable:
The array will no longer exist when the function returns, so any attempt to use it from outside will result in undefined behaviour.
Now, to answer the main question, how to properly return an array from a function?
One option is to use
std::vector:Another is to use
std::array: