I was trying to define a AbstractOptions struct that would hold some options that are used later on for a different class Operator with a concrete implementation ImplementedOptions. As a default value for Operators default constructor I would like to use a previously defined const ImplementedOptions DEFAULT_OPTIONS {1, 2}. Additionally the options member of Operator should be modifiable.
My setup is something like this:
struct AbstractOptions {
AbstractOptions() {};
AbstractOptions(int val1, int val2) : _val1(val1), _val2(val2) {};
AbstractOptions(const AbstractOptions &options) : _val1(options._val1), _val2(options._val2) {};
int _val1;
int _val2;
virtual void implementation() const = 0;
};
struct ImplementedOptions : AbstractOptions{
ImplementedOptions(int val1, int val2 = 4) : _val1(val1), _val2(val2) {};
void implementation() const {/*whatever implementation*/};
};
const ImplementedOptions DEFAULT_OPTIONS {0,1};
class Operator {
public:
Operator() : _options(DEFAULT_OPTIONS) {};
//ERROR -----^^^^^^^^: "Binding reference of type 'AbstractOptions' to value of type 'const ImplementedOptions' drops 'const' qualifier"
private:
AbstractOptions &_options;
};
This fails with the specified error. I would expect that the implemented copy constructor would copy the default and use that to create the instance but this seems not to be the case. If I would specify _options as const AbstractOptions &_options this would work as expected but I would like to be able to modify this struct.
It seems like there is a better way to achieve what I am trying but I'm somewhat at a loss. How could I refactor the code?
Edit: removed some copy-paste errors.