-> 在 C++ 中是什么意思?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4113365/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 14:33:42  来源:igfitidea点击:

What does -> mean in C++?

c++

提问by Eric Brotto

回答by Charles Salvia

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_ptrand unique_ptr, as well as STL container iterators, overload this operator to mimic native pointer semantics.

它也可以被重载以执行特定对象类型的特定功能。像shared_ptrand这样的智能指针unique_ptr,以及 STL 容器迭代器,重载这个操作符来模仿原生指针语义。

For example:

例如:

std::map<int, int>::iterator it = mymap.begin(), end = mymap.end();
for (; it != end; ++it)
    std::cout << it->first << std::endl;

回答by J-16 SDiZ

a->bmeans (*a).b.

a->b是指(*a).b

If ais a pointer, a->bis the member bof which apoints to.

如果a是一个指针a->b是成员b,其中a点。

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

a也可以是类似对象的指针(如 avector<bool>的存根)覆盖运算符。

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

(如果你不知道指针是什么,你还有另一个问题)

回答by Paul Michalik

  1. Access operator applicable to (a) all pointer types, (b) all types which explicitely overload this operator
  2. Introducer for the return type of a local lambda expression:

    std::vector<MyType> seq;
    // fill with instances...  
    std::sort(seq.begin(), seq.end(),
                [] (const MyType& a, const MyType& b) -> bool {
                    return a.Content < b.Content;
                });
    
  3. introducing a trailing return type of a function in combination of the re-invented auto:

    struct MyType {
        // declares a member function returning std::string
        auto foo(int) -> std::string;
    };
    
  1. 访问运算符适用于 (a) 所有指针类型,(b) 显式重载此运算符的所有类型
  2. 本地 lambda 表达式的返回类型介绍器:

    std::vector<MyType> seq;
    // fill with instances...  
    std::sort(seq.begin(), seq.end(),
                [] (const MyType& a, const MyType& b) -> bool {
                    return a.Content < b.Content;
                });
    
  3. 结合重新发明的函数引入尾随返回类型auto

    struct MyType {
        // declares a member function returning std::string
        auto foo(int) -> std::string;
    };
    

回答by Chris Card

x->y can mean 2 things. If x is a pointer, then it means member y of object pointed to by x. If x is an object with operator->() overloaded, then it means x.operator->().

x->y 可能意味着两件事。如果 x 是指针,则表示 x 指向的对象的成员 y。如果 x 是 operator->() 重载的对象,则表示 x.operator->() 。

回答by mepcotterell

member b of object pointed to by a a->b

a a->b 指向的对象的成员 b

回答by Hyman

The ->operator, which is applied exclusively to pointers, is needed to obtain the specified field or method of the object referenced by the pointer. (this applies also to structsjust for their fields)

->需要专门应用于指针的运算符来获取指针所引用对象的指定字段或方法。(这也structs仅适用于他们的领域)

If you have a variable ptrdeclared as a pointer you can think of it as (*ptr).field.

如果将变量ptr声明为指针,则可以将其视为(*ptr).field.

A side node that I add just to make pedantic people happy: AS ALMOST EVERY OPERATOR you can define a different semantic of the operator by overloading it for your classes.

我添加的一个侧节点只是为了让学究的人高兴:作为几乎每个操作员,您可以通过为您的类重载它来定义操作符的不同语义。