Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 9th, 2012  |  syntax: None  |  size: 0.34 KB  |  hits: 13  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Can const member function return a non-const pointer to a data member?
  2. class A
  3. {
  4.     public:
  5.         ...
  6.         int *foo() const
  7.         {
  8.             return _px;
  9.         }
  10.     private:
  11.         int *_px;
  12. }
  13.        
  14. int *& foo() const {return _px;}
  15.        
  16. int * const & foo() const {return _px;}
  17.        
  18. void f(const A& a)
  19. {
  20.   *(a.foo()) = 42; // damn!
  21. }