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

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -