A postfix-expression followed by the member-selection operator (.) and a name is another example of a postfix-expression. The first operand of the member-selection operator must have class or class reference type, and the second operand must identify a member of that class.
The result of the expression is the value of the member, and it is an l-value if the named member is an l-value.
A postfix-expression followed by the member-selection operator (–>) and a name is a postfix-expression. The first operand of the member-selection operator must have type pointer to a class object (an object declared as class, struct, or union type), and the second operand must identify a member of that class.
The result of the expression is the value of the member, and it is an l-value if the named member is an l-value. The –> operator dereferences the pointer. Therefore, the expressions e–>member and (*e).member (where e represents an expression) yield identical results (except when the operators –> or * are overloaded).
When a value is stored through one member of a union but retrieved through another member, no conversion is performed. The following program stores data into the object U
as int but retrieves the data as two separate bytes of type char:
#include <iostream.h>
void main()
{
struct ch
{
char b1;
char b2;
};
union u
{
struct ch uch;
short i;
};
u U;
U.i = 0x6361; // Bit pattern for "ac"
cout << U.uch.b1 << U.uch.b2 << "\n";
}