development:cpp:differences

Difference between “::” “.” and “->” in c++

  • - > for accessing object member variables and methods via pointer to object
Foo *foo = new Foo();
foo->member_var = 10;
foo->member_func();
  • . for accessing object member variables and methods via object instance
Foo foo;
foo.member_var = 10;
foo.member_func();
  • :: for accessing static variables and methods of a class/struct or namespace. It can also be used to access variables and functions from another scope (actually class, struct, namespace are scopes in that case)
int some_val = Foo::static_var;
Foo::static_method();
int max_int = std::numeric_limits<int>::max();
Enter your comment:
B R E E C
 
  • development/cpp/differences.txt
  • Last modified: 2019-10-31 09:04
  • (external edit)