It's pretty obvious that you want to return a new object to the caller, which does not need to be referenced. For this purpose, it is easiest to return an object by value.
OtherClass Class::my_method( ... ) { return OtherClass( ... ); }
Then in the calling code you can build a new object as follows.
{ Class m( ... ); OtherClass n( m.mymethod( ... ) ); }
This avoids any worries about returning links to temporary requests or requiring the client to remove the administrator of the returned pointer. Please note that this requires that your object be copied, but this is a legitimate and usually implemented optimization for a copy that should be avoided when returning by value.
You will only need to consider a generic pointer or similar if you require joint ownership or for the object to have a lifetime outside the scope of the calling function. In this latter case, you can leave this decision to the client and still return by value.
eg.
{ Class m( ... ); // Trust me I know what I'm doing, I'll delete this object later... OtherClass* n = new OtherClass( m.mymethod( ... ) ); }
Charles Bailey
source share