+ 1
Can we do explicit conversion to primary type with user-defined data type??
2 Antworten
+ 8
Something like this?
#include <iostream>
class Integer
{
public: int v;
Integer(int x) { v = x; }
operator int(){ return v; }
};
int main()
{
int primitive_type;
Integer user_defined_type(3939);
primitive_type = user_defined_type;
std::cout << primitive_type;
}
+ 1
In c++, you can't ACTUALLY define your own data type.
Using the "typedef" keyword will only let you CHANGE the name of a pre-defined data type.
Example::
typedef int Number;
the above expression only tells C++ to form a new Keyword called "Number" and use it as another word for "int"
So after typing this ::
typedef int Number;
you'll now be able to do stuff like --->
Number age=17;
Hope it helps