Make a variable read only, but still accessible by clients in C++? -


is there way this:

class example { public:    const int dontmodifyme;     example() {       // setup dontmodifyme..       dontmodifyme = getvaluefordontmodifyme(earliersetup);    } }  example ex; cout << ex.dontmodifyme; // works ex.dontmodifyme = 4 // error 

if dontmodifyme didn't need setup, use member initialization list. there way around doesn't require explicit getter/setter methods?

something have used in past along lines of:

class example {     int m_thevalue; public:     const int &thevalue = m_thevalue;  } 

this allows edit value internally via m_thevalue while keeping constant interface available in "public" realm. similiar in effect of getter/setter method, doesn't require actual usage of said methods.


Comments

Popular posts from this blog

c - Bitwise operation with (signed) enum value -

xslt - Unnest parent nodes by child node -

YouTubePlayerFragment cannot be cast to android.support.v4.app.Fragment -