can I convert a class to int in c++?
for example:
class UserInt
{
.....
};
and is there a way to do this:
UserInt ui = 5;
int a = ui;
?
thanks!
Yes. Use
operator int(). For example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
class UserInt {
public:
UserInt(int i) : m_i(i) {}
operator int() const {
return m_i;
}
// ...
private:
int m_i;
};
// ...
UserInt ui = 10;
int a = ui;
|
thanks a lot!
Topic archived. No new replies allowed.