title: Return a const reference and non const member function call
body: <p>I am a novice C++ programmer trying to hone my skills on a pet project and i have the following problem.</p>
  
  <p>I have a class named System that contains a single Object of another class Agent. I would like an external source to call the methods of the Agent class object in the following manner:</p>
  
  <pre><code>system.agent().exploration(3.0);
  </code></pre>
  
  <p>Where exploration(3.0) sets an internal variable of agent to 3.0. In order to do this i return the Agent object by reference:</p>
  
  <pre><code>Agent&amp; agent() {return agent;}
  </code></pre>
  
  <p>Although this works, it is still possible for someone to change the agent as followed:</p>
  
  <pre><code>system.agent() = Agent(1.0,2.0);
  </code></pre>
  
  <p>In my case this is unwanted behavior. To fix this is figured i could return Agent as const reference:</p>
  
  <pre><code>const Agent&amp; agent() {return agent;}
  </code></pre>
  
  <p>However this code doesn't compile as the method exploration(float e) is non-const (it modifies an internal variables of the agent object).</p>
  
  <p>What is the right way to make the agent object public interface available to the outside world through the System class without allowing someone to completely replace the agent object?</p>