+ 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?

3rd Jun 2017, 8:43 AM
Emi
Emi - avatar
3 ответов
+ 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.
3rd Jun 2017, 8:52 AM
Hatsy Rei
Hatsy Rei - avatar
+ 2
@Hatsy Rei thx for your answer 😊
3rd Jun 2017, 9:17 AM
Emi
Emi - avatar
+ 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
4th Jun 2017, 11:39 AM
Anu Agrawal
Anu Agrawal - avatar