Meaning and usage of the C++ center arrow operator

  • 2020-06-15 09:56:27
  • OfStack

C++ center arrow operator -> , equivalent to combining the demerit and member accessor operators at 1, in other words,

p->func() and (*p).func() It means one thing.

Such as:


class A
{
public:
func();
}

class B
{
 A *p = new A();
*p.a(); 
// Or use p->a . 2 These are equivalent and more concise 
}
[

To understand is to say, minus > Object pointer provides a more convenient way to access object members.

]

The operator -> Is a member operator that points to the struct and combines the direction from left to right.

Examples are as follows:


#include<stdio.h>
//  Declarations and definitions of structures 
struct
{
  char name[10];
  int age;
  char sex;
}person;
void main()
{
  int i;
  //  Here is the point to the struct member operator ( -> The use of the) 
  i = person->age; //  Extract structure member variables age And assign to the variable i
}

conclusion


Related articles: