function() const {};

Hello,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
using namespace std;

class MyClass {
  mutable int i;
  int j;
public:
  int geti() const {
    return i; // ok
  }

  void seti(int x) const {
    i = x; // now, OK.
  }
};

int main()
{
  MyClass ob;

  ob.seti(1900);
  cout << ob.geti();

  return 0;
}


What can you use the const for at a function?

Thank you.
It is used to indicate that, a particular method won't be modifing the object. Normally, you wouldn't be able to write seti(int x) function, but since you have used mutable keyword, you are allowed to change the value of i inside a constant function. Check here: http://stackoverflow.com/questions/105014/c-mutable-keyword and http://www.cplusplus.com/forum/beginner/61312/
Thank you very much. Now I understand:)
Topic archived. No new replies allowed.