With its diverse set of capabilities and features, C++ is a widely used programming language. One of the fundamental concepts in C++ is the use of operators to perform various operations on data. This article covers both dot (.) and arrow (->) operators in detail.
What is the Dot (.) Operator?
To access the members of an object or structure, the dot (.) operator is used. Simply specify the object or structure variable, followed by a dot (.) and the member’s name.
Syntax and Usage
The syntax for using the dot operator is straightforward, we simply write the name of the object or structure variable, followed by a dot, and then the name of the member or attribute we want to access.
Similarly, if we have a structure variable with a member, we can also access it using the dot operator as follows:
In both cases, the dot operator allows us to access a specific member or attribute of an object or structure variable, allowing us to retrieve or manipulate its data as needed.
What is the Arrow (->) Operator?
The arrow (->) operator can also access members of an object or a structure using a pointer. It is used with a pointer to an object or structure followed by an arrow (->) and then the member’s name.
Syntax and Usage
The syntax for using the arrow (->) operator is as follows:
Differences between Dot (.) and Arrow (->) Operators
The main difference between the dot (.) operator and the arrow (->) operator is how they can access object members. The dot (.) operator is used with an object or structure variable, while the arrow (->) operator is used with a pointer to an object or structure.
Accessing Members Using Dot (.) Operator
To access members of an object or structure using the dot (.) operator, we need to have an object or structure variable. Here’s an example:
using namespace std;
struct Person {
string name;
int age;
};
int main() {
Person p1;
p1.name = "SAM";
p1.age = 30;
cout <<"Name: " <<p1.name << endl;
cout <<"Age: " <<p1.age << endl;
return 0;
}
Accessing Members Using Arrow (->) Operator
The arrow (->) operator can access members of an object or structure via a pointer. Therefore, it’s necessary to have a pointer to the object or structure. Here’s an example:
using namespace std;
struct Person {
string name;
int age;
};
int main() {
Person *p1 = new Person;
p1->name = "SAM";
p1->age = 30;
cout <<"Name: " <<p1->name << endl;
cout <<"Age: " <<p1->age << endl;
delete p1;
return 0;
}
Note: One common mistake when using the dot (.) and arrow (->) operators is to use the wrong operator for the given situation. For example, using the dot (.) operator with a pointer or using the arrow (->) operator with an object or structure variable will result in a compilation error.
Conclusion
The dot (.) and arrow (->) operators can access members of an object in C++. Among the both C++ operators, the main difference lies in how they are used, with the dot (.) operator being used with an object or structure variable and the arrow (->) operator being used with a pointer to an object or structure.