Return by Pointer, or, return by Reference?

I want to return a pointer to an object from a function to any function that called it, but I am not sure whether that object being pointed to is merely a copy. IE: Can I then make changes to that objects member variables in the function that it was returned to?
OR
Should I instead return a reference to an object if I want to make changes to that object? If so, I was wondering how to do return by reference. IE: How is the function declared and how do I access the object reference once it is returned to a function?

Thanks for any help. Tried googling this without success.
generally, returning a pointer or reference is bad form unless it's referring to something the class owns (for example, the [] operator returning a reference to one of its vars is OK).

The reason it's bad form is it raises ownership issues. Does the calling function own the object, or did the function create it? Who is responsible for destroy the object?


To give a more clear answer, why don't you give me an example of the kind of function you're looking to write (like what it does, etc).
The objects have been created globally and not in a function.
In various functions the objects will be dynamically selected by a user to undergo changes, ie; member variables altered etc and / or the function may automatically make changes to an object without user input.

To do this I need a findfunction which (when called from another function), will determine which object
is 'available' for change based on a number of other circumstances. The findfunction then returns a 'pointer to object' , back to whatever calling function, and then any changes are made to that object in that calling function.

These change(s) to the objects variables are not 'temporary' nor limited to within the 'calling function scope' only. I'm not sure of the correct terminology here but, what I mean is that the changes are... I suppose... 'permanent' changes until the object is perhaps changed again later in another function or in the same function at a later point.

Because the changes to the object must take affect on the object and not a copy, I was unsure about returning a pointer or a reference. I am basically learning both but as yet have not seen any tutorials on returning a reference. I s'pose it cant be too different, but just want to make sure. Thanks.


Last edited on
okay then either should be fine. Returning a pointer would allow you to return a null pointer (if for example your findfuntion couldn't find an appropriate object), but other than that there's only syntacital differences.
No worries. Thanks Disch.
Topic archived. No new replies allowed.