+ 1

Can we do explicit conversion to primary type with user-defined data type??

20th Jun 2018, 5:52 AM
Suraj Manchala
Suraj Manchala - avatar
2 Respostas
+ 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; }
20th Jun 2018, 6:57 AM
Hatsy Rei
Hatsy Rei - avatar
+ 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
20th Jun 2018, 6:32 AM
Dlite
Dlite - avatar