int + C-style //What happen? Why? //Что произойдет? Почему так? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

int + C-style //What happen? Why? //Что произойдет? Почему так?

What happen if i do this: string str("123" + 1); cout << str; // 23 Why this happen? Что произойдет при сложении int и string? string str("123" + 1); cout << str; // 23 Update: isn't need string cout << "123"+1 // 23 Строка уменьшается на число символов равное переменной int начиная с первого символа строки. Почему это происходит? Что вообще происходит? https://code.sololearn.com/cCXwlfk62sWn/?ref=app

19th Sep 2018, 7:37 PM
Lair
1 Answer
+ 2
You initialize your strings with char arrays (C-style strings), which means you pass a pointer to the constructor. That pointer points to the beginning of the array. Now if you add one to that pointer, it points no longer to the first character, but instead to the second one. Therefore the string is initialized with "23" only. The same principle applies for the other cases.
19th Sep 2018, 7:49 PM
Shadow
Shadow - avatar