C++ 我可以用什么来代替箭头运算符 `->`?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/221346/
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-27 13:49:52  来源:igfitidea点击:

What can I use instead of the arrow operator, `->`?

c++pointers

提问by P-A

What is the arrow operator (->) a synonym for?

箭头运算符 ( ->) 的同义词是什么?

回答by Greg Hewgill

The following two expressions are equivalent:

以下两个表达式是等价的:

a->b

(*a).b

(subject to operator overloading, as Konrad mentions, but that's unusual).

(受运算符重载的影响,正如康拉德所提到的,但这是不寻常的)。

回答by Konrad Rudolph

a->bis generally a synonym for (*a).b. The parenthesises here are necessary because of the binding strength of the operators *and .: *a.bwouldn't work because .binds stronger and is executed first. This is thus equivalent to *(a.b).

a->b通常是 的同义词(*a).b。此处的括号是必需的,因为运算符的绑定强度*.: *a.b不起作用,因为.绑定更强并且首先执行。这因此等价于*(a.b)

Beware of overloading, though: Since both ->and *can be overloaded, their meaning can differ drastically.

不过要小心重载:因为->*都可以重载,所以它们的含义可能会有很大不同。

回答by P-A

The C++-language defines the arrow operator (->) as a synonym for dereferencing a pointer and then use the .-operator on that address.

C++ 语言将箭头运算符 ( ->)定义为取消引用指针的同义词,然后.在该地址上使用- 运算符。

For example:

例如:

If you have a an object, anObject, and a pointer, aPointer:

如果您有一个对象anObject, 和一个指针aPointer

SomeClass anObject = new SomeClass();
SomeClass *aPointer = &anObject;

To be able to use one of the objects methods you dereference the pointer and do a method call on that address:

为了能够使用对象方法之一,您可以取消引用指针并对该地址进行方法调用:

(*aPointer).method();

Which could be written with the arrow operator:

可以用箭头运算符编写:

aPointer->method();

The main reason of the existents of the arrow operator is that it shortens the typing of a very common task and it also kind of easy to forgot the parentheses around the dereferencing of the pointer. If you forgot the parentheses the .-operator will bind stronger then *-operator and make our example execute as:

箭头运算符存在的主要原因是它缩短了一个非常常见的任务的输入时间,并且在取消引用指针时也很容易忘记括号。如果您忘记了括号,.-operator 将绑定得比 *-operator 强,并使我们的示例执行为:

*(aPointer.method()); // Not our intention!

Some of the other answer have also mention both that C++ operators can be overload and that it is not that common.

其他一些答案也提到了 C++ 运算符可以重载并且它并不常见。

回答by Johannes Schaub - litb

In C++0x, the operator gets a second meaning, indicating the return type of a function or lambda expression

在 C++0x 中,运算符有第二个含义,表示函数或 lambda 表达式的返回类型

auto f() -> int; // "->" means "returns ..."

回答by Tetha

I mostly read it right-to-left and call "in"

我主要是从右到左阅读并调用“in”

foo->bar->baz = qux->croak

becomes:

变成:

"baz in bar in foo becomes croak in qux."

“baz in bar in foo 在 qux 中变成 croak。”

回答by Tryb Ghost

->is used when accessing data which you've got a pointer to.

->在访问您有指针的数据时使用。

For example, you could create a pointer ptr to variable of type int intVar like this:

例如,您可以像这样创建一个指向 int intVar 类型变量的指针 ptr:

int* prt = &intVar;

You could then use a function, such as foo, on it only by dereferencing that pointer - to call the function on the variable which the pointer points to, rather than on the numeric value of the memory location of that variable:

然后,您可以仅通过取消引用该指针来在其上使用函数,例如 foo - 在指针指向的变量上调用函数,而不是在该变量的内存位置的数值上调用:

(*ptr).foo();

Without the parentheses here, the compiler would understand this as *(ptr.foo())due to operator precedence which isn't what we want.

如果这里没有括号,编译器会将此理解为*(ptr.foo())运算符优先级,这不是我们想要的。

This is actually just the same as typing

这实际上和打字一样

ptr->foo();

As the ->dereferences that pointer, and so calls the function foo()on the variable which the pointer is pointing to for us.

由于->取消引用该指针,因此foo()为我们调用指针指向的变量上的函数。

Similarly, we can use ->to access or set a member of a class:

同样,我们可以使用->访问或设置类的成员:

myClass* ptr = &myClassMember;
ptr->myClassVar = 2; 

回答by Zhang

You can use -> to define a function.

您可以使用 -> 来定义函数。

auto fun() -> int
{
return 100;
}

It's not a lambda. It's really a function. "->" indicates the return type of the function.

它不是 lambda。这真的是一个函数。“->”表示函数的返回类型。