Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is "operator->()" in C++?

I came across a C++ code that has something like operator->() being called. Below is the code snippet, should someone please explain it.

template <typename T>
bool List<T>::operator == (const List& rhs)const
{
  return (this == &rhs) || (root_.operator->() == rhs.root_.operator->());
}

Please note that root_ is object of another class of which full code is not available to me.

EDIT: I just explored the code and found that root_ is actually a custom implementation of a smart pointer. It has operator -> overloaded in it to dereference the smart pointer and get the actually pointer's value.

like image 706
Haider Avatar asked Jun 08 '13 13:06

Haider


People also ask

What is “/=” operator in C++?

e. “/=”: This operator is a combination of ‘/’ and ‘=’ operators. This operator first divides the current value of the variable on left by the value on the right and then assigns the result to the variable on the left. (a /= b) can be written as (a = a / b) If initially, the value stored in a is 6.

What is a += B operator in C++?

b. “+=”: This operator is combination of ‘+’ and ‘=’ operators. This operator first adds the current value of the variable on left to the value on the right and then assigns the result to the variable on the left. (a += b) can be written as (a = a + b) If initially value stored in a is 5.

What are the types of operators in C?

An operator is a symbol that tells the compiler to perform specific mathematical or logical functions. C language is rich in built-in operators and provides the following types of operators −. Arithmetic Operators. Relational Operators. Logical Operators. Bitwise Operators. Assignment Operators.

What are arithmetic operators in C++?

Arithmetic Operators These operators are responsible for performing arithmetic or mathematical operations like addition (+), subtraction (-), multiplication (*), division (/), the remainder of the division (%), increment (++), decrement (–). Unary Operators: This type of operator works with a single value (operand) like ++ and –.


2 Answers

When you have an object, you can access its attributes via object.attr. When you have a pointer, you can access the attributes of the object it's pointing on by using the -> operator like so: ptr->attr.

So far this is the default behavior in C. However, the -> operator can be overloaded - i.e., overridden like any function can. You can define your own behavior for a class so that object-> will mean whatever you want. However, I don't believe that in this context, the operator was overloaded. The weird syntax is because you can't just do this:

if lhs-> == rhs->

Since the -> operator must be followed by something. So the way to do it is to use the explicit, no-sugar, name for this function, i.e., operator->, and call it like a function (hence the parenthesis).

So:

return (this == &rhs) || (root_.operator->() == rhs.root_.operator->());

This line's meaning is "return true if the my object equals to the object on the left or if our _root attributes point to objects which are equal among themselves.".

like image 111
Amir Rachum Avatar answered Oct 13 '22 00:10

Amir Rachum


What you're asking about is called the Structure dereference operator : T::operator ->(); It comes from c, and in c++ it can be overloaded.

It selects an element through pointer and used in the way it is used in your example it simply returns the address of the instance (root_).

The address is then used to compare identity of instances (do the addresses match?)

like image 21
emesx Avatar answered Oct 12 '22 22:10

emesx