
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
What is the difference between the dot (.) operator and -> in C++?
The dot and arrow operator are both used in C++ to access the members of a class. They are just used in different scenarios. In C++, types declared as class, struct, or union are considered "of class type". So the following refers to all three of them.
- a.b is only used if b is a member of the object (or reference[1] to an object) a. So for a.b, a will always be an actual object (or a reference to an object) of a class.
- a→b is essentially a shorthand notation for (*a).b, ie, if a is a pointer to an object, then a→b is accessing the property b of the object that a points to.
Note that . is not overloadable. → is overloadable operator, so we can define our own function(operator→()) that should be called when this operator is used. so if a is an object of a class that overloads operator→ (common such types are smart pointers and iterators), then the meaning is whatever the class designer implemented.
[1] References are, semantically, aliases to objects, so I should have added "or reference to a pointer" to the #3 as well. However, I thought this would be more confusing than helpful, since references to pointers (T*&) are rarely ever used.
example
#include<iostream> class A { public: int b; A() { b = 5; } }; int main() { A a = A(); A* x = &a; std::cout << "a.b = " << a.b << "\n"; std::cout << "x->b = " << x->b << "\n"; return 0; }
Output
This will give the output −
5 5
- Related Articles
- What is dot operator in C++?
- What is dot operator in Java?
- What is the difference between equals() method and == operator in java?
- Difference between "new operator" and "operator new" in C++?
- What is the difference between new operator and object() constructor in JavaScript?
- What is the difference between –Match, -Like and –Contains Operator in PowerShell?
- What's the difference between assignment operator and copy constructor in C++?
- What is the difference between operator and method on Python set?
- What is the difference between MySQL ISNULL() function and IS NULL operator?
- What is the difference Between AND, OR operator in MySQL while Retrieving the Rows?
- Difference between the and$ operator in php
- Difference between the AND and && operator in php
- What is the significant difference between MySQL LIKE and equal to (=) operator?
- Difference between == and is operator in python.
- Difference between the Ternary operator and Null coalescing operator in php
