I wrote a class with an overloaded conversion operator to do something, if a member variable is readed (e.g. calculate a checksum, read a stored value from an EEPROM, etc).
This works fine, if a simple data type is used.
template <typename T>
class Storage
{
private:
public:
T var;
Storage(const T &value)
: var(value)
{ }
// 'read' variable
operator const T& () const
{
// ... do something
return var;
}
// 'write' variable
const T& operator = (const T &value)
{
var = value;
// ... do something
return var;
}
};
Storage<int> setupValue(34500);
int test1 = setupValue;
If I use a struct
instead, it also works, when reading the whole struct
:
typedef struct
{
int min;
int start;
int max;
} setup_t;
Storage<setup_t> setupValues({35100, 39000, 42000});
setup_t test2 = setupValues;
But, when I read only a single member variable of that struct
, the overloaded operator functions are not called:
int test3 = setupValues.var.start;
Is there a way, to always call the overloaded operator function?
The 'do something' part should be executed, even if I read only a member of the setup struct.