What should a C++ getter return

You can provide both const and non-const versions:

MyType       & MyClass::getMyType()       { return mMyType; }
MyType const & MyClass::getMyType() const { return mMyType; }

I wouldn't provide a pointer version, since that implies that the return value might be the null pointer, which it can never be in this instance.

The real point, however, is that you are basically giving the caller direct access to the internal object. If this is your intent, then you may as well make the data member public. If it isn't, then you will need to work harder to hide the object.

One option is to retain the MyType const & accessor, but provide more indirect means to modify the internal object (setMyType(…) or something more tailored to the semantics that you are trying to express at the level of the containing class).


In general, you should prefer return by value, unless you explicitly want to guarantee that the reference will designate a member (which exposes part of your implementation, but is desirable in cases like std::vector<>::operator[]). Returning a reference prevents later changes in class, since it means that you cannot return a calculated value. (This is especially important if the class is designed to be a base class, since returning a reference creates this restriction for all derived classes.)

The only time you should return by pointer is if a lookup or something is involved, which may return in having to return a null pointer.

Returning a reference to const may be a valid optimization, if the profiler indicates performance problems here, and the call site can also deal with a const reference (no modification of the returned value, no problems with lifetime of object). It must be weighed against the additional constraints on the implementation, of course, but in some cases, it is justified.


I would always return a const reference. If you need to modify the value it is returning just use a setter function.


Return by value, such as: MyType MyClass::getMyType() { return mMyType; } should be avoided as you will copy the content of your object. I do not see the gain you could have but I see the drawbacks on performance.

Return by const reference: const MyType& MyClass::getMyType() { return mMyType; } is more generaly used this way:

const MyType& MyClass::getMyType() const { return mMyType; }
MyType& MyClass::getMyType() { return mMyType; }

Always provide the const version. The non-const version is optional as it means that someone can modify your data. It is what I would encourage you to use.

Return by address: MyType* MyClass::getMyType() { return &mMyType; } is mostly used when the data is optionally there. It often has to be checked before being used.

Now, your use case, I would strongly advice not to keep a pointer save for more than a scope. I can often lead to ownership problems. I you have to do so, take a look to shared_ptr.

For your examples, there is two cases:

a. savedPointer won't be valid any more after the closing brace.

b,c, and d. savedPointer is valid after the closing brace, but beware it should not outlive myClass.