title: Which method is better for implementing get/set?
body: <p>There are two methos for implementing get/set.</p>
  
  <p>Method 1:</p>
  
  <p>Define get and set separately.</p>
  
  <pre><code>class my_class
  {
    // ...
  };
  
  class main_class
  {
  public:
  
    my_class get_data() const
    {
      return m_data;
    }
  
    void set_data(my_class value)
    {
      m_data = value;
    }
  
  private:
  
    my_class m_data;
  };
  </code></pre>
  
  <p><strong>Note</strong>: In this method get is fast enough: <a href="http://cpp-next.com/archive/2009/08/want-speed-pass-by-value" rel="noreferrer">http://cpp-next.com/archive/2009/08/want-speed-pass-by-value</a></p>
  
  <p>And another method is (Method 2):</p>
  
  <p>Define two get bodies, First const and another non const.</p>
  
  <pre><code>class my_class
  {
    // ...
  };
  
  class main_class
  {
  public:
  
    const my_class&amp; get_data() const
    {
      return m_data;
    }
  
    my_class&amp; get_data() // Works like set.
    {
      return m_data;
    }
  
  private:
  
    my_class m_data;
  };
  </code></pre>
  
  <p>Using these methods:</p>
  
  <pre><code>void main()
  {
    main_class cls;
  
    // For method 1.
    my_class data;
    data = cls.get_data();
    cls.set_data(data);
  
    // For method 2.
    const my_class data1;
    my_class data2;
    data1 = cls.get_data();  // const get invoked.
    cls.get_data() = data2; // Like set beacuase non const get invoked.
  
  }
  </code></pre>
  
  <p>My question which of these methods for implementing get/set is better?</p>
  
  <p>Do you know a better method?</p>
  
  <hr>
  
  <p><strong>Edit</strong>:
  For answers that believe Method 1 is better, what do you say in below situation:</p>
  
  <pre><code>void main()
  {
    main_class cls;
  
    // For method 1.
    cls.get_data().do_something_else(); // Not effictive for cls, because data losts.
  
    // For method 2.
    cls.get_data().do_something_else(); // Effictive for cls.    
  }
  </code></pre>