
- 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 ?-->? operator in C++?
There is no such operator in C++. Sometimes, we need to create wrapper types. For example, types like unique_ptr, shared_ptr, optional and similar. Usually, these types have an accessor member function called .get but they also provide the operator→ to support direct access to the contained value similarly to what ordinary pointers do.
The problem is that sometimes we have a few of these types nested into each other. This means that we need to call .get multiple times or to have a lot of dereference operators until we reach the value.
Something like this −
wrapper<wrapper<std::string>> wp; wp.get().get().length(); wp.get()->length();
This can be a bit ugly. If we can replace one .get() with an arrow, it would be nice if we could replace the second .get() as well. For this, the C++98 introduced a long arrow operator.
wrapper<wrapper<std::string>> wp; wp--->length();
What if we have another layer of wrapping? Just make a longer arrow.
wrapper<wrapper<wrapper<std::string>>> wp; wp----->length();
The long arrow is not a single operator, but a combination of multiple operators. In this case, a normal -> operator and the postfix decrement operator --.
So, when we write wp----→length(), the compiler sees ((wp--)--)→length().
If we define the postfix -- to be the same as the dereference operator, we get the long arrow, and the even longer arrow operators −
template <typename T> class wrapper { public: T* operator->() { return &t; } T& operator--(int) { return t; } private: T t; };
- Related Articles
- What is the operator precedence in C#?
- What is a Ternary operator/conditional operator in C#?
- What is the purpose of ‘is’ operator in C#?
- What is the scope resolution operator in C#?
- What is sizeof operator in C++?
- What is Comma operator in C++?
- What is dot operator in C++?
- What is arrow operator in C++?
- What is pointer operator & in C++?
- What is Pointer operator * in C++?
- What is ternary operator in C#?
- What is Cast Operator () in C#?
- What is the use of sizeof Operator in C#?
- What is the purpose of ‘as’ operator in C#?
- What is double address operator(&&) in C++?
