code_junkie

What are the semantics of a const member function?

Nov 11th, 2011
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.67 KB | None | 0 0
  1. class object
  2. {
  3. int get_value(int n) const
  4. {
  5. ...
  6. }
  7.  
  8. ...
  9.  
  10.  
  11. object x;
  12.  
  13. int a = x.get_value(1);
  14. ...
  15. int b = x.get_value(1);
  16.  
  17. class X
  18. {
  19. int data;
  20. mutable boost::mutex m;
  21. public:
  22. void set_data(int i)
  23. {
  24. boost::lock_guard<boost::mutex> lk(m);
  25. data=i;
  26. }
  27. int get_data() const // we want to be able to get the data on a const object
  28. {
  29. boost::lock_guard<boost::mutex> lk(m); // this requires m to be non-const
  30. return data;
  31. }
  32. };
  33.  
  34. int something() const { return m_pSomeObject->NextValue(); }
  35.  
  36. class Foo
  37. {
  38. public:
  39. int bar() const
  40. {
  41. static int x = 0;
  42. return x++;
  43. }
  44. };
Add Comment
Please, Sign In to add comment