I have a class containing a set of attributes, which I want to be able to have default values get from another instance (named parent).
If the instance does not have a parent, then it is assigned default values.
But if the instance have a parent, then I would like it to default its attributes to the parent’s ones, with the restriction that if the parent attributes are modified then the object attributes can also get the new value.
Given this restriction, I can’t simply copy the values from the parent.
Thus I think I have two methods:
- Use pointers: if the pointer is nullptr then use the parent value.
- Use a bool field for each attribute, indicating the status.
Both methods should work, but they also have incovenient:
- The first will need manual instantiation even if it can be simplified with shared_ptr or unique_ptr. But this will lead to additional instructions, thus slower code
- The second will lead to additional memory consumption : each instance will take at least one more byte (most of time 2, 4 or even 8 bytes) per attribute.
My question is: Is there any other way to implement such behavior? Or what is the best solution?
Then use a bitfield or bit mask to use one single bit for each attribute. Nothing more efficient than that.