I have something like
class X
{
private:
someclass obj;
public:
someclass& get()
{
return obj;
}
};
I dont want to make obj public, so i get a reference of it and pass it around to functions.
Is this a good/ok practice or is it outright evil ?
If you pass a non-const reference as in your code then
objcan be modifed from outside your class. If you wish that it cannot be changed from outsideXyou must return a const ref,Note with this case it is worth adding another const after the function name
which tells the compiler that calling
get()on anXwon’t change its internal state. (This is not true of your example)