-> 在 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
What does -> mean in C++?
提问by Eric Brotto
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++?
->在 C++ 中是什么意思?
回答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
- Access operator applicable to (a) all pointer types, (b) all types which explicitely overload this operator
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; });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; };
- 访问运算符适用于 (a) 所有指针类型,(b) 显式重载此运算符的所有类型
本地 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; });结合重新发明的函数引入尾随返回类型
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 Core Xii
http://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B#Member_and_pointer_operators
http://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B#Member_and_pointer_operators
a -> bis member bof object pointed to by a
a -> b被构件b对象的通过指向一个
回答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.
我添加的一个侧节点只是为了让学究的人高兴:作为几乎每个操作员,您可以通过为您的类重载它来定义操作符的不同语义。

