The question is simple but i can't find a solution.
class foo
{
public:
operator int()
{
return 5;
}
};
foo* a = new foo();
int b = a;
Is it possible to implement that behavior?
You can't. Conversion operators need to be members of a class, but foo*
is not a user-defined class type, it's a pointer type (besides, int b = *a
would work).
The best thing you can do is to use an utility function that does the casting.
You can, by explicitly calling the operator:
foo* a = new foo();
int b = a->operator int();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With