c++operatorsexpressionscope-resolution-operator

C++ When do I use -> or ::


I know that in C++, you use either -> or :: instead of . in language such as C# to access object's values, e.g. button->Text or System::String^, but I don't know when I should use -> or ::, and it is very frustrating as it causes me many compiler errors. I would be very grateful if you could help. Thanks :)


Solution

  • I try to show an examples of some usages of ::, . and ->. I hope it helps:

    int g;
    
    namespace test
    {
    
      struct Test
      {
         int x;
         static void func();
      };
    
      void Test:: func() {
         int g = ::g;
      }
    
    }
    
    int main() {
    
      test::Test v;
      test::Test *p = &v;
    
      v.x = 1;
      v.func();
      p->x = 2;
      p->func();
    
      test::Test::func();
    
    }