c++ - const keyword and duplicate method signature -
i started learning c++, switching java environment.
when reading through boost examples found following 2 methods defined in class:
const char* data() const { return data_; } char* data() { return data_; } there 2 things confuse me.
first reserved word const, think understand here. first const refers char* means cannot change value of pointer. second const tells me calling function not make changes state of object calling. correct interpretation?
second point of confusion why 1 have 2 methods same name , signature. how compiler know 1 meant call? how know whether allowed change data after calling data() without know of 2 called?
the first function returns pointer constant data. const @ end of function signature indicates function not modify , class data members.
the second function returns pointer mutable data. caller can use pointer modify class member variable.
search web , "const correctness".
Comments
Post a Comment