using const with function

Feb 28, 2022 at 4:15pm
closed account (oj2wbRfi)
what is the purpose of using const with function

1
2
  void readData () {}
  void readData const () {}
Feb 28, 2022 at 4:24pm
Feb 28, 2022 at 5:00pm
For that case, the const goes after the () and the function has to be a class member function. In the above, the const is before the () !

@Ivanka - is this a misprint as that is invalid syntax?
Feb 28, 2022 at 5:19pm
It is a promise that the function will not modify the object that it's called on.
 
obj.readData(); // will not modify obj 

It's similar to marking reference parameters as const.
 
void foo(const T&);
 
foo(obj); // will not modify obj 
Last edited on Feb 28, 2022 at 5:22pm
Topic archived. No new replies allowed.