c++ function syntax/prototype - data type after brackets -
i familiar c/c++ standard function declarations. i've seen this:
int myfunction(char parameter) const
the above hypothetical example , don't know if makes sense. i'm referring part after parameter. const. this?
a more real example:
wxgridcellcoordsarray getselectedcells() const
this can found here text const
doing @ end of line?
the const keyword, when shown after function, guarantees function caller no member data variables altered.
for instance given class,
// in header class node { public: node(); void changevalue() const; ~node(); private: int value; };
// in .cpp
void node::changevalue() const { this->value = 3; // error out because modifying member variables }
there exception rule. if declare member data variable mutable, can altered regardless if function declared const. using mutable rare situation object declared constant, in practice has member data variables need option change. 1 potential example of use caching value may not want repeat original calculation. typically rare... aware of it.
for instance given class,
// in header class node { public: node(); void changevalue() const; ~node(); private: mutable int value; };
// in .cpp
void node::changevalue() const { this->value = 3; // not error out because value mutable }
Comments
Post a Comment