+ 5
String problem in C++
everyone know that string is a keyword of C++, but I found that string can also be a variable name in C++. It can be a keyword and variable in the same time like this string string = "hello"; and compiler run it with no problem can someone explain to me, why can a keyword can be a variable or the keyword string actually is not a keyword, but a non-keyword keyword, how he declare a variable?
3 Réponses
+ 15
string is actually not a reserved keyword. It is a class, and the object of a class can have the same name as the class itself.
Please refer:
https://stackoverflow.com/questions/3601295/why-string-string-is-allowed-and-int-int-is-not-allowed-by-compiler
Good question btw.
+ 2
@Hatsy Rei thx for your answer 😊
+ 1
string is not a keyword ,'string' is defined by the class 'string' that you included here #include <string>. may be that's by you can use string as your variable name