If you don't want to use an
std::vector
then the only other option is to return return both a pointer to the array and the number of elements.
int MyFunc(int** pointerToFill)
{
*pointerToFill = new int;
return numElements;
}
HRESULT MyFunc(int** pointerToFill, int& iNumElements)
{
*pointerToFill = new int;
iNumElements = numElements;
return SUCCESS;
}
I don't think there really is a 'smarter' way.
Replacing the regular array with an
std::vector
shouldn't really cause that many problems though. You can still use it exactly the same as an array but instead of having an extra variable to store the number of elements it will contain the number itself and you don't need to worry so much about allocating and freeing memory. To save a little typing you can also set up a
typedef
.
typedef std::vector<int> MyIntArra