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.
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.
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.
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.
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 –.
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.".
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?)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With