random question: can you change the data type of a variable? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

random question: can you change the data type of a variable?

Like without assigning the value to a new variable of another type?

15th May 2018, 6:16 PM
Q++
4 Answers
+ 4
In C++, variable types are fix when you declare them. That doesn't prevent you from using the data as a different type because you're allowed to cast it as anything you want.
15th May 2018, 6:28 PM
John Wells
John Wells - avatar
+ 1
You can do things like encapsulation for example, where class B inherits drom class A and you assing pointer of A to sometihng of type B. You can laso change thing like char to int or vice versa due to the code that character use. It is hard for me to think about other things that may be useful. You can assign any pointer to any other tbh, but I do not think that this will make you any good tho
15th May 2018, 6:20 PM
Paul
+ 1
Alright thanks :)
15th May 2018, 6:29 PM
Q++
0
yeah sure
16th May 2018, 3:34 PM
Aljninja
Aljninja - avatar