How do I set what an object returns when just the object name is used? For instance, a string will return a value when just the name is used, without using any particular function. Is this done by overloading?
How do I set what an object returns when just the object name is used? For instance, a string will return a value when just the name is used, without using any particular function. Is this done by overloading?
You mean something like this?
Code:#include <iostream> class Something { public: operator char(){ return 'c'; } }; void someFunction(char val) { std::cout<<val; } int main() { Something s; someFunction(s); std::cin.get(); }
Woop?
Be careful with that, though. While it seems cool, it can often lead to unintended code compiling but not working as expected. It is usually better to just go ahead and make a named function.
Ill try that and see if I run into any problems, thanks.