The answer depends on what you are doing and who is responsible for the release.
The first method: select in a heap and return. Anyone who has ever called this function will be responsible for deleting the returned pointer.
SomeObject* constructObject () { SomeObject* obj = new SomeObject (); return obj; }
Then in some other function
void functionThatNeedsObject () { SomeObject* obj = constructObject ();
Second method: return the link. You must be careful not to go out of scope by returning local or temporary variables.
Do not do this:
int& DoubleValue(int nX) { int nValue = nX * 2; return nValue;
You can return references to member variables or variables passed as arguments to the function.
SomeStruct& RefFunction(SomeStruct& nX, SomeStruct& nY) { return nX; } //nX is still in scope because it was passed to us
crazyx
source share