Structure, Union, and Class Member Operators: . and ->
postfix-expression
**.**identifier
postfix-expression
–**>**identifier
A “member-selection expression” refers to members of structures, unions, and classes. Such an expression has the value and type of the selected member.
This list describes the two forms of the member-selection expressions:
In the first form, postfix-expression represents a value of struct, class, or union type, and identifier names a member of the specified structure, union, or class. The value of the operation is that of identifier 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 identifier names a member of the specified structure, union, or class. The value is that of identifier and is an l-value.
The two forms of member-selection expressions have similar effects.
In fact, an expression involving the member-selection operator (->
) is a shorthand version of an expression using the period (.) if the expression before the period consists of the indirection operator (*) applied to a pointer value. Therefore,
expression->
identifier
is equivalent to
**(*expression) .**identifier
when expression is a pointer value.
Example
In the following example, the structure Dates
and one object, myDates
, is declared. The members of the myDates
object are initialized with values for the beginning of 1995.
// Example of the structure operator
struct Dates {
CString sGregorian;
int nJulian;
};
Dates myDates;
Dates.nJulian = 1;
Dates.sGregorian = "01/01/95";