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
Post a Comment