Suppose I have a mouse position watcher, which should let the user extract the mouse position.  Should the mouse position be set in the method and returned:Mouse_Position Mouse_Watcher::get_mouse_position( void ) const{   Mouse_Position return_val = Mouse_Position( this->mp );   // memory stuff so the value can escape this scope and not cause segmentation faults   return return_val;}Mouse_Position mp = Mouse_Position();mp = mouse_man::get_mouse_position();or the slightly easier way I've found:void Mouse_Watcher::sync_mouse_position( Mouse_Position & destination ) const{   // the class handles copying   // if the stuff is primitive and uses this syncing method,   //   seems like a ton of avoided memory mishaps   destination.sync_x( this->mp );   destination.sync_y( this->mp );}Mouse_Position mp = Mouse_Position();mouse_man::sync_mouse_position( mp );// at this point mp has position valuesI've been used to setting values using the return, but I don't like unnecessarily including memory management if I can help it.  Should I go back to using returns to keep up with standards?
Should I set values using the return or by giving a reference?
c++;memory
Unsurprisingly, this depends on the purpose of the function and the nature of the type it's retrieving/extracting. But it's usually easy to make a choice based on the properties of the type.1) The value is a primitive such as int or charThen the choice is effectively between:int x = getInt();andint x;setInt(x);I would strongly prefer returning the value, because:It just looks more natural/normal/intuitive/readable/etc.Now that the function takes an argument, it's possible that its behavior depends on the current value of that argument. Semantically, the current value of x is completely irrelevant to what this function does, so why give the function access to it?Copying and assigning ints costs basically nothing. Odds are both versions get optimized to the same instructions anyway.2) The value is a trivial struct/class (eg, no private data, methods or memory management)I assume your Mouse_Position class falls into this category. This case is basically identical to #1, because a class like this behaves much the same as a primitive.More formally, when I say this class is like a primitive value, I'm saying this class has value semantics. That means a lot of things, such as if two objects of that class have the same data members, they are completely interchangeable for all intents and purposes. This is a very nice property to have when it comes to reasoning about your code.3) The value has methods/private data/memory management, but it implements RAII and still technically has value semanticsThis is where STL container classes go. A std::vector definitely contains private data, including a pointer to memory it has to manually manage, but its constructors, destructors and copy operations are implemented in such a way that it still behaves like a primitive value in many ways. In particular, two vectors that compare equal are still interchangeable; it doesn't matter what region of memory they happen to be storing their values in.In this case, whether you return by value or take a reference parameter mostly comes down to efficiency. In C++ you normally don't want to copy classes like this if you don't have to, because that implies copying all the private data or memory being managed (which may involve allocating more memory). However, move semantics in modern C++ make it so that returning a std::vector does not necessarily perform a copy anymore, so simply returning it by value is becoming more acceptable (I won't try to explain the exact conditions under which copies do/don't happen before/after C++11; that's too complicated).4) The value has no trace of value semantics, and preserving its identity is importantThese are typically classes that represent external dependencies like files, databases, network connections, peripheral devices, rendering contexts and so on. It often does not make sense to copy these objects (you can't give your computer more keyboards by copying a Keyboard class) so functions operating on them basically have to operate on pointers/references to them as a simple matter of correctness. Obviously here the decision is made for you.Short answer: Don't use pointers/references when they're completely unnecessary. Simply copying/returning values often results in more intuitive and much better-behaved code.