Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does -> mean in C++? [duplicate]

Tags:

c++

Possible Duplicates:
What is the difference between the dot (.) operator and -> in C++?
What is the arrow operator (->) synonym for in C++?

The header says it all.

What does -> mean in C++?

like image 253
Eric Brotto Avatar asked Nov 06 '10 13:11

Eric Brotto


People also ask

What does -> this mean in C?

An Arrow operator in C/C++ allows to access elements in Structures and Unions. It is used with a pointer variable pointing to a structure or union.

What is -> Next pointer?

It means "Give me the value of the thing pointed at the address stored at ptr ". In this example, ptr is pointing to a list item so ptr->next returns the value of the object's next property. Follow this answer to receive notifications.

What is the --> operator in C C++?

C++ Relational Operators Here, > is a relational operator. It checks if a is greater than b or not. If the relation is true, it returns 1 whereas if the relation is false, it returns 0.

Why do we use this -> C++?

The 'this' pointer is passed as a hidden argument to all nonstatic member function calls and is available as a local variable within the body of all nonstatic functions. 'this' pointer is not available in static member functions as static member functions can be called without any object (with class name).


2 Answers

It's to access a member function or member variable of an object through a pointer, as opposed to a regular variable or reference.

For example: with a regular variable or reference, you use the . operator to access member functions or member variables.

std::string s = "abc"; std::cout << s.length() << std::endl; 

But if you're working with a pointer, you need to use the -> operator:

std::string* s = new std::string("abc"); std::cout << s->length() << std::endl; 

It can also be overloaded to perform a specific function for a certain object type. Smart pointers like shared_ptr and unique_ptr, as well as STL container iterators, overload this operator to mimic native pointer semantics.

For example:

std::map<int, int>::iterator it = mymap.begin(), end = mymap.end(); for (; it != end; ++it)     std::cout << it->first << std::endl; 
like image 120
Charles Salvia Avatar answered Sep 18 '22 18:09

Charles Salvia


a->b means (*a).b.

If a is a pointer, a->b is the member b of which a points to.

a can also be a pointer like object (like a vector<bool>'s stub) override the operators.

(if you don't know what a pointer is, you have another question)

like image 24
J-16 SDiZ Avatar answered Sep 17 '22 18:09

J-16 SDiZ