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?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
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 = *awould work).The best thing you can do is to use an utility function that does the casting.