Home > Backend Development > C++ > body text

What does → mean in c++?

下次还敢
Release: 2024-05-01 10:00:30
Original
874 people have browsed it

In C, the "→" operator (member access operator) is used to access members of a class, including: Data member access: Returns a reference to a data member. Member function call: Returns a function pointer pointing to the member function. The "→" operator only works on pointers to instances of classes, for value types the . operator should be used.

What does → mean in c++?

The “→” operator in C

In C, the “→” operator is called "Member access operator", which is used to access class members. Specifically, it is used to access data members and member functions in a class.

Grammar

The syntax of the "→" operator is:

<code class="cpp">objectPtr->member</code>
Copy after login

Among them:

  • ##objectPtr is a pointer to an instance of the class.
  • member is a data member or member function in a class.

Usage: Data member access

When the "→" operator is used to access a data member, it returns a reference to the data member. The following example shows how to access the

name data member of a class:

<code class="cpp">class Person {
public:
    string name;
};

int main() {
    Person person;
    person.name = "John Doe";

    string& name = person->name;
    cout << name << endl; // 输出:"John Doe"
}</code>
Copy after login

Usage: Member function call

When the "→" operator is used When a member function is called, it returns a function pointer. The following example shows how to call the

getName member function of a class:

<code class="cpp">class Person {
public:
    string getName() { return name; }
    string name;
};

int main() {
    Person person;
    person.name = "John Doe";

    string (*getName)(Person*) = person->getName;
    string name = getName(&person);
    cout << name << endl; // 输出:"John Doe"
}</code>
Copy after login

Note

    The "→" operator can only be used A pointer to an instance of a class.
  • For value types, the "→" operator cannot be used, only the
  • . operator can be used.
  • The "→" operator has higher priority than the "*" and
  • . operators.

The above is the detailed content of What does → mean in c++?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
c++
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template