I have a class member defined as:
someType* X;
and I get it like:
someType* getX() {return x;}
I would like to get the value rather than the pointer i.e.:
someType getX() {return x;} //this is wrong
what is the proper syntax for this? How do I get the value rather than the pointer?
Note though that this returns
xby value, i.e. it creates a copy ofxat each return*. So (depending on whatsomeTypereally is) you may prefer returning a reference instead:Returning by reference is advisable for non-primitive types where the cost of construction may be high, and implicit copying of objects may introduce subtle bugs.
*This may be optimized away in some cases by return value optimization, as @paul23 rightly points out below. However, the safe behaviour is not to count on this in general. If you don’t ever want an extra copy to be created, make it clear in the code for both the compiler and human readers, by returning a reference (or pointer).