class - Missing convention for C++ accessors? -
i've read community wiki quetion 3 different conventions accessor methods , supmetwhat surprised not see following convention:
const unsigned& amount() const { return _amount; } unsigned& amount() { return _amount; } true, it's not quite same seamless being able avoid parentheses altogether () - (i feel) idea - it's still something; right?
it defeats purpose of accessors. if provide 2 functions, might make data member public , done it.
edit:
just make things clear: there are cases using c style struct appropriate solution. in cases, make data members public, , don't worry accessors. classes significant behavior, on other hand, won't have accessors @ all, or few. part, internal state not reflected directly @ public interface (and state is read only). time you'd have need accessors classes data, must enforce invariants across data,
(and it's worth: if data logically attribute of class, use:
int amount() const { return myamount; } void amount( int newvalue ) { myamount = newvalue; } for getters of values not logically attributes, however, i'll use getamount().)
Comments
Post a Comment