Doubt about const used in member functions.

In some cases we have "const" in two differents parts of the function
1)
 
      const char *Leer() { return cad; }

2)
 
     int Lee() const { return A; }


Why is it? Thanks a lot
1) the function returns a pointer to const char. You can read but you can't modify what the pointer points to.

2) the Lee() function promise not to modify the object that it is being called on.
So in 1) there isn't any prommise of not to modify the parameter?
If by parameter you mean the object that the function is being called on then that is correct. The function probably should be marked const though, like in 2), so that it can be called on const objects as well.

1
2
3
4
const Class obj;
const char* p = obj.Leer(); // This will not work because Leer() 
                            // gives no promise not to modify
                            // the const object obj. 
Last edited on
Topic archived. No new replies allowed.