next up previous
Next: How do I modify Up: class and struct Previous: How do I prevent

When should I use const methods?

When a method isn't going to modify a class, it should be const. This prevents from modifying attributes unwantedly, and reduces significantly errors:
struct A
  int   a;
  bool  f(int i) const
    if (a = i)     // error. f shouldn't  modify a.
      return true;
    return false;

Alexis Angelidis (PhD) 2005-01-11