This question already has an answer here:
这个问题已经有了答案:
- What can I use instead of the arrow operator, `->`? 5 answers
- 我可以用什么代替箭头操作符,' -> ' ?5个回答
What is the difference between the dot (.) operator and -> in C++?
点(.)运算符和->在c++中的区别是什么?
14 个解决方案
#1
271
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.
由于*和的结合强度,上面的括号是必要的。操作符。
*foo.bar()
wouldn't work because Dot (.
) operator is evaluated first (see operator precedence)
*foo.bar()不能工作,因为首先对点(.)运算符求值(参见运算符优先级)
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++?
还可以看到:在c++中,箭头操作符(->)的同义词是什么?
#2
60
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 ->
但是一直输入()的s是很困难的,所以他们开发了->作为表示相同内容的快捷方式。如果您正在访问对象或对象引用的属性,请使用。如果您正在通过指针访问对象的属性,请使用->
#3
54
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 (ptr->field is equivalent to (*ptr).field)
当应用到指针箭头运算符时,编辑器就等于将点算符应用到pointee (ptr->字段相当于(*ptr).field)
#4
26
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
.
箭头操作符就像点,只是首先去引用一个指针。bar()调用对象foo上的方法bar(), foo->bar调用指针foo指向的对象上的方法bar。
#5
12
pSomething->someMember
is equivalent to
相当于
(*pSomething).someMember
#6
11
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
#7
10
The target. dot works on objects; arrow works on pointers to objects.
目标。点对象工作;箭头指向对象的指针。
std::string str("foo");
std::string * pstr = new std::string("foo");
str.size ();
pstr->size ();
#8
3
Note that the -> operator cannot be used for certain things, for instance, accessing 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.
}
#9
2
-> use when you have pointer . use when you have structure (class) when you want point attribute that belong to structure use . structure.attribute
when want point to attribute that have refference to memory by pointer use -> :
-当你有指针时使用>。当您需要属于结构使用的点属性时,可以使用结构(类)。结构。当需要指向的属性时,指针使用->对内存进行了响应。
pointer->method;
or same as:
(*pointer).method
#10
1
It's simple, whenever you see
这很简单,只要你看到
x->y
know it is the same as
知道它是一样的
(*x).y
#11
1
The . (dot) operator is usually used to get a field / call a method from an instance of class (or a static field / method of a class).
的。操作符通常用于从类实例(或类的静态字段/方法)获取字段/调用方法。
p.myField, p.myMethod() - p instance of a class
p。myField, p.myMethod() - p类的实例
The -> (arrow) operator is used to get a field / call a method from the content pointed by the class.
->(箭头)运算符用于从类所指向的内容中获取字段/调用方法。
p->myField, p->myMethod() - p points to a class
p->myField, p->myMethod() - p指向一个类
#12
1
The -> is simply syntactic sugar for a pointer dereference,
>是一个简单的语法糖,
As others have said:
当别人说:
pointer->method();
方法指针- >();
is a simple method of saying:
是一种简单的说法:
(*pointer).method();
(*指针).method();
For more pointer fun, check out Binky, and his magic wand of dereferencing:
想要获得更多的教鞭乐趣,请访问Binky和他的魔杖。
http://www.youtube.com/watch?v=UvoHwFvAvQE
http://www.youtube.com/watch?v=UvoHwFvAvQE
#13
1
The simplest difference between the two is that "->" dereferences a pointer before it goes to look at that objects fields, function etc. whereas "." doesn't dereference first. Use "->" when you have a pointer to an object, and use "." when you're working with the actual instance of an object.
两者之间最简单的区别是,“->”在指针去查看对象字段、函数等之前将其取消引用,而“.”并不是首先取消引用。当你有对象的指针时,使用“->”,当你使用对象的实际实例时使用“。”
Another equivalent way of wrinting this might be to use the dereferencing "*" on the pointer first and then just use the ".". We skip middleman by using "->".
另一种类似的方法是首先在指针上使用“*”,然后使用“”。我们通过使用“->”来跳过中间商。
There are other differences, but the other answers have covered this extensively.
还有其他的不同之处,但是其他的答案已经涵盖了这个范围。
If you have a background in Java this might confuse you, since, in Java, everything is pointers. This means that there's no reason to have symbol that doesn't dereference your pointer first. In c++ however you gotta be a little more careful with remembering what is and what isn't a pointer, and it might be a good idea to label them with the prefix "p_" or simply "p".
如果您有Java的背景,这可能会让您感到困惑,因为在Java中,一切都是指针。这意味着没有理由不首先取消对指针的引用。但是,在c++中,要记住什么是指针,什么不是指针,要更小心一点,最好用前缀“p_”或简单的“p”来标记它们。
#14
1
The -> operator is used when we are working with a pointer and the dot is used otherwise. So if we have a struct class like:
当我们使用指针时,使用->操作符,而不使用点。如果我们有一个struct类
struct class{ int num_students; int yr_grad; };
and we have an instance of a class* curr_class (class pointer), then to get access to number of students we would do
我们有一个类的实例* curr_class(类指针),然后我们要访问学生的数量
cout << curr_class->num_students << endl;
In case we had a simple class object , say class_2016, we would do
如果我们有一个简单的类对象,比如class_2016,我们会这么做
cout << class_2016.num_students << endl;
For the pointer to class the -> operator is equivalent to
对于类的指针,->操作符等价于
(*obj).mem_var
Note: For a class, the way to access member functions of the class will also be the same way
注意:对于类,访问类的成员函数的方式也是相同的
#1
271
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.
由于*和的结合强度,上面的括号是必要的。操作符。
*foo.bar()
wouldn't work because Dot (.
) operator is evaluated first (see operator precedence)
*foo.bar()不能工作,因为首先对点(.)运算符求值(参见运算符优先级)
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++?
还可以看到:在c++中,箭头操作符(->)的同义词是什么?
#2
60
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 ->
但是一直输入()的s是很困难的,所以他们开发了->作为表示相同内容的快捷方式。如果您正在访问对象或对象引用的属性,请使用。如果您正在通过指针访问对象的属性,请使用->
#3
54
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 (ptr->field is equivalent to (*ptr).field)
当应用到指针箭头运算符时,编辑器就等于将点算符应用到pointee (ptr->字段相当于(*ptr).field)
#4
26
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
.
箭头操作符就像点,只是首先去引用一个指针。bar()调用对象foo上的方法bar(), foo->bar调用指针foo指向的对象上的方法bar。
#5
12
pSomething->someMember
is equivalent to
相当于
(*pSomething).someMember
#6
11
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
#7
10
The target. dot works on objects; arrow works on pointers to objects.
目标。点对象工作;箭头指向对象的指针。
std::string str("foo");
std::string * pstr = new std::string("foo");
str.size ();
pstr->size ();
#8
3
Note that the -> operator cannot be used for certain things, for instance, accessing 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.
}
#9
2
-> use when you have pointer . use when you have structure (class) when you want point attribute that belong to structure use . structure.attribute
when want point to attribute that have refference to memory by pointer use -> :
-当你有指针时使用>。当您需要属于结构使用的点属性时,可以使用结构(类)。结构。当需要指向的属性时,指针使用->对内存进行了响应。
pointer->method;
or same as:
(*pointer).method
#10
1
It's simple, whenever you see
这很简单,只要你看到
x->y
know it is the same as
知道它是一样的
(*x).y
#11
1
The . (dot) operator is usually used to get a field / call a method from an instance of class (or a static field / method of a class).
的。操作符通常用于从类实例(或类的静态字段/方法)获取字段/调用方法。
p.myField, p.myMethod() - p instance of a class
p。myField, p.myMethod() - p类的实例
The -> (arrow) operator is used to get a field / call a method from the content pointed by the class.
->(箭头)运算符用于从类所指向的内容中获取字段/调用方法。
p->myField, p->myMethod() - p points to a class
p->myField, p->myMethod() - p指向一个类
#12
1
The -> is simply syntactic sugar for a pointer dereference,
>是一个简单的语法糖,
As others have said:
当别人说:
pointer->method();
方法指针- >();
is a simple method of saying:
是一种简单的说法:
(*pointer).method();
(*指针).method();
For more pointer fun, check out Binky, and his magic wand of dereferencing:
想要获得更多的教鞭乐趣,请访问Binky和他的魔杖。
http://www.youtube.com/watch?v=UvoHwFvAvQE
http://www.youtube.com/watch?v=UvoHwFvAvQE
#13
1
The simplest difference between the two is that "->" dereferences a pointer before it goes to look at that objects fields, function etc. whereas "." doesn't dereference first. Use "->" when you have a pointer to an object, and use "." when you're working with the actual instance of an object.
两者之间最简单的区别是,“->”在指针去查看对象字段、函数等之前将其取消引用,而“.”并不是首先取消引用。当你有对象的指针时,使用“->”,当你使用对象的实际实例时使用“。”
Another equivalent way of wrinting this might be to use the dereferencing "*" on the pointer first and then just use the ".". We skip middleman by using "->".
另一种类似的方法是首先在指针上使用“*”,然后使用“”。我们通过使用“->”来跳过中间商。
There are other differences, but the other answers have covered this extensively.
还有其他的不同之处,但是其他的答案已经涵盖了这个范围。
If you have a background in Java this might confuse you, since, in Java, everything is pointers. This means that there's no reason to have symbol that doesn't dereference your pointer first. In c++ however you gotta be a little more careful with remembering what is and what isn't a pointer, and it might be a good idea to label them with the prefix "p_" or simply "p".
如果您有Java的背景,这可能会让您感到困惑,因为在Java中,一切都是指针。这意味着没有理由不首先取消对指针的引用。但是,在c++中,要记住什么是指针,什么不是指针,要更小心一点,最好用前缀“p_”或简单的“p”来标记它们。
#14
1
The -> operator is used when we are working with a pointer and the dot is used otherwise. So if we have a struct class like:
当我们使用指针时,使用->操作符,而不使用点。如果我们有一个struct类
struct class{ int num_students; int yr_grad; };
and we have an instance of a class* curr_class (class pointer), then to get access to number of students we would do
我们有一个类的实例* curr_class(类指针),然后我们要访问学生的数量
cout << curr_class->num_students << endl;
In case we had a simple class object , say class_2016, we would do
如果我们有一个简单的类对象,比如class_2016,我们会这么做
cout << class_2016.num_students << endl;
For the pointer to class the -> operator is equivalent to
对于类的指针,->操作符等价于
(*obj).mem_var
Note: For a class, the way to access member functions of the class will also be the same way
注意:对于类,访问类的成员函数的方式也是相同的