C++ Member operator

C++ Operators

The . (dot) operator and the -> (arrow) operator are used to reference members of classes, structures, and unions.

The dot operator is applied to an actual object. The arrow operator is used with a pointer to an object. For example, suppose the following structure:

struct Employee {
  char first_name[16];
  int  age;
} emp;

(.)dot operator

The following code assigns the value "zara" to the member of the object emp.first_nameMembers:

strcpy(emp.first_name, "zara");

(->) arrow operator

If p_emp is a pointer to an object of type Employee, then to assign the value "zara" to the member of the object emp via the pointer, use:first_nameFor members, you need to write the following code:

strcpy(p_emp->first_name, "zara");

-> is called the arrow operator; it is composed of a minus sign followed by a greater-than sign.

In short, use the dot operator when accessing members of a structure, and use the arrow operator when accessing members of a structure through a pointer.

C++ Operators

other extensions