pretty specific question here!
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.
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.
However, I don't think this is allowed inside a dynamic container such as a vector or list.
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.
The message is this: "error: non-static const member const double* const x
, can't use default assignment operator"
So, there is a problem with copying the class when I do a "push_back" on to a vector - right?
Here is an example 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& rad; // This is initialized in the constructor list
};
int main(...){
...
std::vector<planet> 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);
...
}