title: How to provide const version of operator -> for a handle class
body: <p>I am trying to overload my operator-> for a handle class to return const and non const pointer, pointing towards a base class.</p>
  
  <p>Looking at the code i posted, in the trial function, if i add in the const keyword, the error message will be</p>
  
  <pre><code>||=== Build: Debug in Const trial (compiler: GNU GCC Compiler) ===|
  C:\Const trial\main.cpp
  ||In function 'bool trial(Cards_pointer)':|
  C:\Const trial\main.cpp|50|error: passing 'const Cards_pointer' as 'this' argument of 'Cards*&amp; Cards_pointer::operator-&gt;()' discards qualifiers [-fpermissive]|
  ||=== Build failed: 1 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|
  </code></pre>
  
  <p>My question is whether it is possible to do so, if yes, may i know what is the correct implementation? </p>
  
  <pre><code>#include &lt;iostream&gt;
  #include&lt;vector&gt;
  #include&lt;stdexcept&gt;
  #include&lt;algorithm&gt;
  using namespace std;
  
  class Cards
  {
  private:
      int x;
  public:
      Cards():x(3) {}
      int rx()const
      {
          return x;
      }
  };
  
  class Cards_pointer
  {
  private:
      Cards* cp;
      size_t* refptr;
  
  public:
  //default constructor
      Cards_pointer():cp(0),refptr(new size_t(1)) {}
      Cards_pointer(Cards*t):cp(t),refptr(new size_t(1)) {}
  //copy constructor
      Cards_pointer (const Cards_pointer&amp;s):cp(s.cp),refptr(s.refptr)
      {
          refptr=s.refptr;
          cp=s.cp;
  //++*refptr;
          *refptr=*refptr+1;
      }
  
      Cards*&amp;operator-&gt;()
      {
          if(cp)
              return cp;
  
          else throw std::runtime_error("uninitialized Cards");
      }
  
  };
  
  bool trial(const Cards_pointer x)
  {
      if(x-&gt;rx()==3)
          return true;
  
      return false;
  }
  
  int main()
  {
      Cards_pointer x=new Cards();
  
      bool cond=trial(x);
  
  }
  </code></pre>