I’m trying to force the code that uses my class to always get one of it’s properties directly — never save it and use it separately. For example, I want the following to be uncompilable.
int x = myObj->prop1;
int y = x + 1;
I want to force them to get the value like this:
int y = myObj->prop1 + 1;
I’m guessing it’s not possible, but I figure if it is then someone here will know.
If you want to know why, it’s complicated but has to do with forcing the code to use my class in a way that is forward-compatible with a future iteration of the class.
You could return some kind of non-copyable proxy instead of an instance of the stored variable. But please consider that for value types, the returned instance is a completely indepent copy of the backing field of your property so the client of your library has no possibility to do ‘any harm’… Hm, maybe you should explain your intent in more detail…