()" in C ++? I came across C ++ - code that calls something like operator-> (). Below is a snippet of code if someone explains...">

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

I came across C ++ - code that calls something like operator-> (). Below is a snippet of code if someone explains this.

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

Note that root_ is an object of another class whose full code is not available to me.

EDIT: I just studied the code and found that root_ is the usual implementation of a smart pointer. It is overloaded by the β†’ operator to dereference the smart pointer and get the actual pointer value.

+8
c ++ operator-overloading
source share
4 answers

When you have an object, you can access its attributes through object.attr . When you have a pointer, you can access the attributes of the object it points to using the -> operator as follows: ptr->attr .

While this is the default behavior in C. However, the -> operator can be overloaded - that is, overridden, like any function. You can define your behavior for the class so that object-> means what you want. However, I do not believe that the operator was overloaded in this context. The strange syntax is that you cannot just do this:

 if lhs-> == rhs-> 

Since the operator -> something should follow. So the way to do this is to use an explicit, sugar-free name for this function, i.e. operator-> , and call it as a function (hence brackets).

So:

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

This string value means "return true if my object is equal to the object on the left or if our _root attributes point to objects that are equal to each other.".

+11
source share

What you are asking is called a structure dereference operator: T::operator ->(); It comes from c , and in C ++ it can be overloaded.

It selects an element through a pointer and is used as it is used in your example, it just returns the instance address ( root_ ).

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

+4
source share

This is an operator overload call -> class.

0
source share

Normally, the β†’ () operator returns a pointer, so this code simply compares two pointers. I think this is an implementation of a linked list, and the == operator compares pointers to the entire list and pointers to the head (root) element

0
source share

All Articles