overloaded const and non-const class methods returning references in C++
const-version of the method get() returns a copy


Why is this a good thing?


  the non-const method get() is const only by contract, (not checked by compiler)
      harder to get a const-reference, though not impossible


What?


  the const-ness of the const method get() is checked by compiler
      copy of the returned object is controlled by the user


What does it matter if it is const if you have to go to the effort of making a copy.


  requires two const_casts which is hard to read


Really!

PS. getters and setters are an anti pattern (unless you are a container).
Exposing the internal implementation like this tightly couples your class to any code that uses it. This makes modifying your code in the future much harder. It is best to make your methods do the work rather than expose the members and do the work outside the class.

class X
{
     Y   value;

     public:
         // Why over complicate things.
         // This has the same functionality.
         Y&amp;       get()       {return value;}
         Y const&amp; get() const {return value;}
 };


OK so you want the code to get the value in one location:

class Z
{
     Y   value;

     public:
         Y&amp;       get()       {return value;}


         // Only really need one cast.
         // We know that get() is not modifying the object.
         // If it was you can't share functions anyway.
         // Thus it is safe to cast away const-ness before calling get.
         // Because you are returning a const&amp; from this function it is
         // fine and you don't need to add const-ness back anyway.
         Y const&amp; get() const {return const_cast&lt;Z&amp;&gt;(*this).get();}
 };