title: Returning non-const attribute reference from const function
body: <p>Is there something wrong with this code?</p>
  
  <pre><code>// Example program
  #include &lt;iostream&gt;
  #include &lt;string&gt;
  #include &lt;vector&gt;
  
  class A
  {
  public:
      A( std::vector&lt;int&gt;&amp; ref ) : data(ref) {}
  
      std::vector&lt;int&gt;&amp; getData() const { return data; }
  
  private:
      std::vector&lt;int&gt;&amp; data;
  };
  
  void modifyA( const A&amp; a )
  {
      a.getData().push_back(3);
  }
  
  int main()
  {
      std::vector&lt;int&gt; foo;
      A a(foo);
      modifyA(a);
      std::cout &lt;&lt; foo.size() &lt;&lt; std::endl;
      std::cout &lt;&lt; foo.front() &lt;&lt; std::endl;
  }
  </code></pre>
  
  <p>It looks good to me, but I'm experiencing undefined behaviour under Android when deploying a much more complex code with similar approach. At some point, a member (a <code>shared_ptr</code>) of the class gets corrupted when the previous member of the class (a <code>std::vector</code>) is modified through such a getter (the <code>shared_ptr</code> becomes <code>NULL</code>, while it was not modified at all). I also saw the program above return unexpected values on <a href="http://cpp.sh" rel="nofollow noreferrer">http://cpp.sh</a> at some point but not consistently. In the end, I could not isolate the problem in a MVCE...</p>
  
  <p>I'm just wondering if returning a non-const reference to a non-const member from a const getter could possibly be a problem.</p>