
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Difference Between Dot Operator and Arrow Operator in C++
In C++, both dot (.) and arrow (->) operators are used to access members of classes, structures, and unions. But they are used in different scenarios based on how the object is being accessed.
In this article, we will learn the differences and uses of these two operators in C++.
Dot Operator (.)
The dot(.) operator is used to access members (variables and functions) of a class, structure, or union when working with its object (not a pointer). It allows you to access and manipulate the object's properties by directly interacting with members of an object.
Syntax
Here is the following syntax for the dot operator in C++.
object_name.member_name;
object_name is the name of an object created from a class or struct.
member_name is the name of a member variable or function defined inside the class or struct.
Example
#include <iostream> using namespace std; class Student { public: string name; int age; }; int main() { Student s; // using dot operator to assign value to the name member s.name = "Amit"; s.age = 25; // using dot operator to access the name member cout << "Name: " << s.name << endl; cout << "Age: " << s.age << endl; return 0; }
Output
Name: Amit Age: 25
The above code shows how the dot operator is used to access and manipulate the members of the Student class for the object 's'.
Arrow Operator (->)
The arrow (->) operator is used to access the members (variables and functions) of a class, structure, or union when working with a pointer to an object. It combines dereferencing the pointer and accessing the object's member. This is useful when working with dynamic memory and object pointers.
Syntax
Here is the following syntax for the arrow operator in C++.
pointer_name->member_name;
Example
#include <iostream> using namespace std; class Student { public: string name; int age; }; int main() { Student* ptr = new Student(); ptr->name = "Aman"; // accessing 'name' using arrow operator ptr->age = 24; // accessing 'age' using arrow operator cout << "Name: " << ptr->name << endl; cout << "Age: " << ptr->age << endl; delete ptr; return 0; }
Output
Name: Aman Age: 24
This code shows the use of the arrow operator (->) to access and modify the members (name and age) of the Student class through a pointer.
Difference between Dot (.) and Arrow (->) Operators
Here is the following comparison table for the dot and arrow operators in C++:
Feature | Dot Operator (.) | Arrow Operator (->) |
Usage | Access members using an object | Access members using a pointer to an object |
Requires | An actual object | A pointer to an object |
Syntax | object.member | pointer->member |
Dereferencing | Not needed | Implicitly dereferences the pointer |
Common with objects | Stack-allocated objects | Heap-allocated objects |
Function Access | obj.show(); | ptr->show(); |
Used when | Object is accessed directly | Object is accessed indirectly through a pointer |