Reputation: 33
Anybody know how to implement a casting rule in C++ for one class?
Let's say I have a class Mark that has a double-type member. And I want this class to be cast-able to double:
Mark m(9);
double d = (double)m;
Is it possible to do this?
Upvotes: 1
Views: 84
Reputation: 38193
You can have operator double()
inside your class. For example
class Mark
{
//...
public:
operator double()
{
return the_member;
}
};
Upvotes: 4