There are at least three questions / comments:
- Array return
- Return item
- Return
std::vector
Array return
You can always return a pointer to an array, but when transferring arrays, you always need to specify the capacity. Unfortunately, functions can only return one value, so you have to return "outside" the parameters from the function:
double * get_position(unsigned int& capacity);
Return item
You can hide the internal representation of {array} by providing a function to return a single element. This gives the advantage of checking borders.
double get_one_position(unsigned int index) { double result = 0.0; if (index >= MAX_CAPACITY) {
Return std::vector
In C ++, we prefer std::vector array. Your problem is one of the reasons for switching. std::vector can be passed and returned from a method. In addition, std::vector supports its own capacity variables, so there is no need to pass throughput along with the array pointer.
std::vector<double> _position(10); std::vector<double> get_positions(void) { return _position; }
Summary
I suggest you first try to hide the implementation of the array from clients or users. If this is not possible, prefer to use std::vector for the array. Finally, use an array.
When you return the array always accompanies its variable capacity.
source share