title: C++ const reference to class variable within vector (or other dynamic container)
body: <p>pretty specific question here!</p>
  
  <p>I have a class which holds data about a planet. For example, it has things like a vector (double x, double y, double z) to hold its position, and a double variable to hold its radius.</p>
  
  <p>I often use references to get a "read only public access" to the private variables. I call a setter method to change the private variable.</p>
  
  <p>However, I don't think this is allowed inside a dynamic container such as a vector or list.</p>
  
  <p>I have tried "constant constant" pointers, with the idea being once initialized in an initialization list, they will not be able to point to anything else or modify the variable. But the same error message appears at compile time.</p>
  
  <p>The message is this: "error: non-static const member <code>const double* const x</code>, can't use default assignment operator"</p>
  
  <p>So, there is a problem with copying the class when I do a "push_back" on to a vector - right?</p>
  
  <p>Here is an example code:</p>
  
  <pre><code>class planet{
  
      private:
  
          double _radius;
  
      public:
  
          // Constructor
          planet() : rad(_radius){
             _radius = 0.0f;
          }
  
          // This is a setter method - works fine
          void setrad(double new_rad){
              _radius = rad;
          }
  
          // This is a better solution to the getter method
          // - does not seem to work with dynamic containers!
          const double&amp; rad; // This is initialized in the constructor list
  };
  
  int main(...){
  
  ...
  
      std::vector&lt;planet&gt; the_planets;
      planet next_planet_to_push;
      next_planet_to_push.setrad(1.0f);
  
      // This causes the error!
      the_planets.push_back(next_planet_to_add);
  
  ...
  
  }
  </code></pre>