C++ 中的点 (.) 运算符和 -> 之间有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1238613/
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 is the difference between the dot (.) operator and -> in C++?
提问by moorthy
What is the difference between the dot (.) operator and -> in C++?
C++ 中的点 (.) 运算符和 -> 之间有什么区别?
回答by SwDevMan81
foo->bar()
is the same as (*foo).bar()
.
foo->bar()
与 相同(*foo).bar()
。
The parenthesizes above are necessary because of the binding strength of the *
and .
operators.
由于*
and.
运算符的绑定强度,上面的括号是必要的。
*foo.bar()
wouldn't work because Dot (.
) operator is evaluated first (see operator precedence)
*foo.bar()
不起作用,因为 Dot ( .
) 运算符首先被评估(请参阅运算符优先级)
The Dot (.
) operator can't be overloaded, arrow (->
) operator can be overloaded.
点 ( .
) 运算符不能重载,箭头 ( ->
) 运算符可以重载。
The Dot (.
) operator can't be applied to pointers.
点 ( .
) 运算符不能应用于指针。
Also see: What is the arrow operator (->) synonym for in C++?
回答by Gordon Gustafson
For a pointer, we could just use
对于指针,我们可以使用
*pointervariable.foo
But the .
operator has greater precedence than the *
operator, so .
is evaluated first. So we need to force this with parenthesis:
但是.
运算符比运算符具有更高的优先级*
,因此.
首先评估。所以我们需要用括号强制它:
(*pointervariable).foo
But typing the ()'s all the time is hard, so they developed ->
as a shortcut to say the same thing. If you are accessing a property of an object or object reference, use .
If you are accessing a property of an object through a pointer, use ->
但是一直输入 () 是很困难的,所以他们开发->
了一个快捷方式来表达同样的事情。如果您正在访问对象的属性或对象引用,请使用.
如果您正在通过指针访问对象的属性,请使用->
回答by Tadeusz Kopec
Dot operator can't be overloaded, arrow operator can be overloaded. Arrow operator is generally meant to be applied to pointers (or objects that behave like pointers, like smart pointers). Dot operator can't be applied to pointers.
点运算符不能重载,箭头运算符可以重载。箭头运算符通常用于指针(或行为类似于指针的对象,如智能指针)。点运算符不能应用于指针。
EDIT
When applied to pointer arrow operator is equivalent to applying dot operator to pointee e.g. ptr->field
is equivalent to (*ptr).field
.
编辑当应用于指针箭头运算符相当于将点运算符应用于指针时,例如ptr->field
相当于(*ptr).field
.
回答by Meredith L. Patterson
The arrow operator is like dot, except it dereferences a pointer first. foo.bar()
calls method bar()
on object foo
, foo->bar
calls method bar
on the object pointed to by pointer foo
.
箭头运算符就像点,除了它首先取消引用一个指针。foo.bar()
调用方法bar()
的对象foo
,foo->bar
调用方法bar
由指针指向的对象上foo
。
回答by Johannes Rudolph
The .
operator is for direct member access.
该.
运营商是直接成员访问。
object.Field
The arrow dereferences a pointer so you can access the object/memory it is pointing to
箭头取消引用一个指针,以便您可以访问它指向的对象/内存
pClass->Field
回答by Tamás Szelei
pSomething->someMember
is equivalent to
相当于
(*pSomething).someMember
回答by ezpz
The target. dot works on objects; arrow works on pointers to objects.
目标。dot 作用于对象;箭头适用于指向对象的指针。
std::string str("foo");
std::string * pstr = new std::string("foo");
str.size ();
pstr->size ();
回答by Am1rr3zA
Use ->
when you have a pointer.
Use .
when you have structure (class).
->
有指针时使用。使用.
时,你有结构(类)。
When you want to point attribute that belongs to structure use .
:
当您想指向属于结构的属性时,请使用.
:
structure.attribute
When you want to point to an attribute that has reference to memory by pointer use ->
:
当您想通过指针指向具有内存引用的属性时,请使用->
:
pointer->method;
or same as:
或相同于:
(*pointer).method
回答by gparent
Note that the -> operator cannot be used for certain things, for instance, accessing operator[].
请注意,-> 运算符不能用于某些事情,例如访问 operator[]。
#include <vector>
int main()
{
std::vector<int> iVec;
iVec.push_back(42);
std::vector<int>* iVecPtr = &iVec;
//int i = iVecPtr->[0]; // Does not compile
int i = (*iVecPtr)[0]; // Compiles.
}
回答by Ali Parr
The -> is simply syntactic sugar for a pointer dereference,
-> 只是指针解引用的语法糖,
As others have said:
正如其他人所说:
pointer->method();
指针->方法();
is a simple method of saying:
是一种简单的说法:
(*pointer).method();
(*pointer).method();
For more pointer fun, check out Binky, and his magic wand of dereferencing:
如需更多指针乐趣,请查看 Binky 和他的解引用魔杖: