As an example in Ruby (1.8)
i = 4
i.class => fixnum
i += 1.3
i.class => float
Can this be achieved in C++?
for instance
template<class T>
struct Number {};
Number<int> foo;
foo.changeTypeToFloat(); // <-- Possible?
// foo now Float?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
C++ is a statically-typed language. A variable has a fixed type. The best you can do at the language level is a conversion to a new variable, e.g.:
Alternatively, you could write a variant class, and have the conversion handled internally. But this would really be syntactic sugar for the above.