I’ve see this done before in languages based on mono, but I’m just curious if this would be possible in c++. If I had the following class
class test {
public:
int foo;
int myfunction();
};
when I try and access the foo variable, is their any way I can make it so it calls myfunction and returns its value?
test t;
cout << t.foo;//instead of printing the value of foo, print the return value of myfunction
Thanks in advance!
I googled this but wasn’t able to find anything.
Or, could it be possible to call a function with no arguments without the parenthesis?
Wow! Thanks for all the input guys. The main reason I wanted no parenthesis is that I’m trying to make this API as similar to that of an old, non-c++ API. Not having two parenthesis after a function in this class might make maintenance a bit harder, but it will saves me hours of answering why Vector2.normalized has to have () after it.
One possible workaround, which is a bit evil, would be to use a macro.
As I alluded to in the comments to your OP though, I think such tricks are terrible from a maintenance perspective, and should be avoided.