Scope resolution operator and const

前端 未结 4 1184
抹茶落季
抹茶落季 2021-01-24 03:27

Let\'s take the following code:

#include  // std::string    
using namespace std;
int main() {
  //const::int i = 42;       -> Error: \"expected         


        
4条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-24 04:12

    ::something tells the compiler to look for something in global namespace, but int is keyword (not defined anywhere), while string (resp. std::string) is class defined in so your code is equivalent to this one

    #include  // std::string    
    using namespace std;
    int main() {
      //const ::int i = 42;       -> Error: "expected id-expression before 'int'"
      const ::string str = "Foo";
    }
    

提交回复
热议问题