I have a configuration class that I want to pass to another class (called Use) in a constructor. I want to store the configuration class inside of the Use class as a private member variable. I would like it to be const.
So far, I have this code:
class Configuration{
private:
int value1_;
public:
Configuration();
Configuration(int value1){value1_=value1;}
int value1() const {
return value1_;
}
};
class Use{
private:
//const me
Configuration config_;
int something_;
public:
Use(Configuration &config){
config_=config;
}
void doSomething(){
something_+=config_.value1();
}
};
I want to const Use::config_, but every way that I try ends in confusing compile errors. How do I do it?
The best way to support this is to use a reference
One item that needs to be a bit clearer in the question is the relationship between
ConfigurationandUseinstances. If there is oneConfigurationwhich is meant to be shared amongst allUseinstances then a reference (as I outlined) is the most appropriate choice. In that scenario though you need to ensure thatConfigurationoutlives allUseinstances that take a reference to it.On the other hand if
Configurationis meant to be one perUseinstance then a non-reference solution is probably more appropriate