Home >Backend Development >C++ >How Do the Dot (.), Arrow (->), and Double Colon (::) Operators Access Members in C ?
), and Double Colon (::) Operators Access Members in C ? " />
Member Access Operators in C : Dot, Arrow, Double Colon
In C , accessing class members can be done using three operators: dot (.), arrow (->), and double colon (::). Each operator serves a distinct purpose, allowing you to understand the context of class member access at a glance.
Double Colon (::): Class Member Access
The :: operator is exclusively used to access members of a class or namespace. The syntax a::b indicates that b is a member of the class or namespace a.
Dot (.): Object Member Access
The . operator is used to access members of an object or a reference to an object. The syntax a.b implies that b is a member of the object a.
Arrow (->): Pointer Member Access
The -> operator is originally a shorthand for (*a).b. However, it can be overloaded by classes. If a is a pointer, then a->b is equivalent to accessing a member of the object the pointer a refers to.
Overloaded Arrow Operator (->)
If a is an object of a class that overloads the -> operator, then invoking the overloaded operator->() function is executed. This provides flexibility in accessing members of complex data structures like smart pointers and iterators.
Additional Notes:
The above is the detailed content of How Do the Dot (.), Arrow (->), and Double Colon (::) Operators Access Members in C ?. For more information, please follow other related articles on the PHP Chinese website!