+ 2
Type conversion in c++?
In Python you can just do this: myString = "123" myInt = int(myString) print(myInt + 1) result: 124 In c++ I've heard of cast operators: string myString = "123"; int myInt = (int) myString; cout << myInt + 1; result: 'invalid type cast from string to int' I've searched Google for the answer and I found these: const_cast<type>(expr) dynamic_cast<type>(expr) static_cast<type>(expr) ... There's so many of them and I need a clear explanation on when to use them and why. I would also like to know when to use: (type) expression and how to cast types onto other types as well, eg: double to int, string to double thanks in advance
1 Réponse
+ 6
So you in c++ is a little diferent from python.
Yes ,you are in the rigth path you can use casting from conversion. Like :
char c = '1' ;
int some = (int) c;
This can be done because char and int both are integer.
But string is different
In this case you want to use a fucntion called stoi
Here a example of using stoi:
https://code.sololearn.com/cNUFd5y7GT70