How could I convert from a class to fundamental data types?

Just like this:

1
2
3
4
5
class myClass{ /* something here */ };

///////////////////
myClass myObj;
int other = (int)myObj;
Last edited on
You need to provide a function to do it.

explicit
1
2
3
4
5
6
7
8
9
class myClass
  {
  ...
  int to_int() const { ... }
  };

...
myClass myObj;
int other = myObj.to_int();

implicit
1
2
3
4
5
6
7
8
9
class myClass
  {
  ...
  operator int() const  { ... }
  };

...
myClass myObj;
int other = (int)myObj;

Good luck!
Topic archived. No new replies allowed.