Member Access Operators: . and ->See Also
Postfix Expressions | C++ Operators | Operator Precedence and Associativity | Classes | Structures | and Unions | Structure and Union Members (C Reference)Grammar
postfix-expression :
postfix-expression . name
postfix-expression –> name
The member access operators . and -> are used to refer to members of structures, unions, and classes. Member access expressions have the value and type of the selected member.
There are two forms of member access expressions:
In the first form, postfix-expression represents a value of struct, class, or union type, and name names a member of the specified structure, union, or class. The value of the operation is that of name and is an l-value if postfix-expression is an l-value.
In the second form, postfix-expression represents a pointer to a structure, union, or class, and name names a member of the specified structure, union, or class. The value is that of name and is an l-value. The –> operator dereferences the pointer. Therefore, the expressions e–>member and (*e).member (where e represents a pointer) yield identical results (except when the operators –> or * are overloaded).
Example
The following example demonstrates both forms of the member access operator.
// expre_Selection_Operator.cpp
// compile with: /EHsc
// Demonstrate member access operators
#include <iostream>
using namespace std;
struct Date {
Date(int i, int j, int k) : day(i), month(j), year(k){}
int month;
int day;
int year;
};