Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

C++ Member Operators

C++ Member Operators

C++ Operators C++ Operators

The dot (.) operator and the arrow (->) operator are used to refer to 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 we have the following structure:



struct Employee {

  char first_name[16];

  int  age;

} emp;

(.) Dot operator

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



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 first_name member of the object emp, you need to write the following code:



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

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

In short, use the dot operator to access a structure's members, and use the arrow operator to access a structure's members through a pointer.

C++ Operators C++ Operators