pointers - C++ Is it possible to return a reference to a private object and preventing change? -
i'm needing return reference private member of class wrote. i'm doing this:
in myclass2.h have following line
myclass* getobj(){return &instance_myclass;}
i make sure code not change value of instance_myclass. make sure, there way return reference read only, can't change value? or this, definition, not possible?
you not return reference pointer.
in order return reference, use:
myclass& getobj() { return instance_myclass; }
if want prevent modifications via reference, make const
(and while you're @ it, make function const
well):
myclass const& getobj() const { return instance_myclass; }
Comments
Post a Comment