What is the function of the '->' in that line?

In summary, the '->' is a symbol used in object-oriented programming languages to access the properties or methods of an object. It is used instead of '.' because it is specifically used for pointers to objects. It cannot be used with non-object variables and is not used in all programming languages. The '->' can also be used in different ways depending on the language and context.
  • #1
NCEE
4
0
I was going through a bit of open source code when I came upon this line
"pTerm = pid->pGain * error;"

What is the function of the '->' in that line?
 
Engineering news on Phys.org
  • #2
pid is a pointer to a class or structure.
pGain is a member of the class or structure.
pid -> pGain means the same as (*pid).pGain (but it looks prettier).
 
  • #3


The '->' in that line is known as the arrow operator or the structure pointer operator. It is used to access a member of a structure or union using a pointer to that structure or union. In this case, the code is accessing the member variable 'pGain' of the structure 'pid' and using it to perform a calculation with the variable 'error'. This operator is commonly used in C and C++ programming languages to access members of structures and unions using pointers. It provides a more efficient way of accessing data compared to using the dot operator, especially when dealing with dynamic memory allocation.
 

FAQ: What is the function of the '->' in that line?

What is the function of the '->' in that line?

The '->' is a symbol used in computer programming languages, particularly in object-oriented languages like C++ and Java, to access the properties or methods of an object. It is known as the "arrow operator" or "member selection operator".

Why is the '->' used instead of '.'?

The '->' is used instead of '.' because it is used to access the properties or methods of a pointer to an object, while the '.' is used to access the properties or methods of the object itself. Pointers are used in programming to store the memory address of an object rather than the actual object itself.

Can the '->' be used with non-object variables?

No, the '->' is specifically used to access the properties or methods of an object. It cannot be used with non-object variables such as integers, strings, or arrays.

Is the '->' used in all programming languages?

No, the '->' is not used in all programming languages. It is mainly used in object-oriented languages like C++ and Java, but other languages may have alternative symbols or syntax for accessing object properties and methods.

Can the '->' be used in different ways?

Yes, the '->' can be used in different ways depending on the language and context. In C++, it is used to access public properties and methods of an object, while in other languages it may have additional uses such as accessing private or protected properties and methods.

Back
Top