"::" "." 和有什么不一样 和 C++ 中的“->”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11902791/
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 "::" "." and "->" in c++
提问by Yoda
Possible Duplicate:
When do I use a dot, arrow, or double colon to refer to members of a class in C++?
I have created the class called Kwadrat and I have three int fields inside. The Code Blocks gives me advice that i can get into the field of the object by ::
, .
and ->
. The arrow is the one that only works, but why? What's the difference between those three?
我创建了一个名为 Kwadrat 的类,里面有三个 int 字段。代码块给了我建议,我可以通过::
,.
和进入对象的领域->
。箭头是唯一有效的箭头,但为什么呢?这三个有什么区别?
#include <iostream>
using namespace std;
class Kwadrat{
public:
int val1, val2, val3;
Kwadrat(int val1, int val2, int val3)
{
this->val1 = val1;
//this.val2 = val2;
//this::val3 = val3;
}
};
int main()
{
Kwadrat* kwadrat = new Kwadrat(1,2,3);
cout<<kwadrat->val1<<endl;
cout<<kwadrat->val2<<endl;
cout<<kwadrat->val3<<endl;
return 0;
}
回答by Andrew
1.->
for accessing object member variables and methods via pointer
to object
1.->
通过pointer
to object访问对象成员变量和方法
Foo *foo = new Foo();
foo->member_var = 10;
foo->member_func();
2..
for accessing object member variables and methods via object instance
2..
用于通过对象访问对象成员变量和方法instance
Foo foo;
foo.member_var = 10;
foo.member_func();
3.::
for accessing static variables and methods of a class/struct
or namespace
. It can also be used to access variables and functions from another scope (actually class, struct, namespace are scopes in that case)
3.::
用于访问 aclass/struct
或 的静态变量和方法namespace
。它还可以用于从另一个作用域访问变量和函数(在这种情况下,实际上类、结构、命名空间都是作用域)
int some_val = Foo::static_var;
Foo::static_method();
int max_int = std::numeric_limits<int>::max();
回答by Heisenbug
In C++ you can access fields or methods, using different operators, depending on it's type:
在 C++ 中,您可以使用不同的运算符访问字段或方法,具体取决于其类型:
- ClassName::FieldName: class public static field and methods
- ClassInstance.FieldName: accessing a public field (or method) through class reference
- ClassPointer->FieldName: accessing a public field (or method) dereferencing a class pointer
- ClassName::FieldName: 类公共静态字段和方法
- ClassInstance.FieldName:通过类引用访问公共字段(或方法)
- ClassPointer->FieldName: 访问公共字段(或方法)取消引用类指针
Note that :: should be used with a class name rather than a class instance, since static fields or methods are common to all instances of a class.
请注意 :: 应该与类名而不是类实例一起使用,因为静态字段或方法对于类的所有实例都是通用的。
class AClass{
public:
static int static_field;
int instance_field;
static void static_method();
void method();
};
then you access this way:
然后你以这种方式访问:
AClass instance;
AClass *pointer = new AClass();
instance.instance_field; //access instance_field through a reference to AClass
instance.method();
pointer->instance_field; //access instance_field through a pointer to AClass
pointer->method();
AClass::static_field;
AClass::static_method();
回答by RageD
Put very simple ::
is the scoping operator, .
is the access operator (I forget what the actual name is?), and ->
is the dereference arrow.
非常简单的::
是作用域运算符,.
是访问运算符(我忘记了实际名称是什么?),->
是取消引用箭头。
::
- Scopes a function. That is, it lets the compiler know what class the function lives in and, thus, how to call it. If you are using this operator to call a function, the function is a static
function.
::
- 作用域一个函数。也就是说,它让编译器知道函数所在的类,从而知道如何调用它。如果您使用此运算符来调用函数,则该函数就是一个static
函数。
.
- This allows access to a member function on an already created object. For instance, Foo x; x.bar()
calls the method bar()
on instantiated object x
which has type Foo
. You can also use this to access public class variables.
.
- 这允许访问已创建对象上的成员函数。例如,Foo x; x.bar()
调用类型为实例bar()
化对象的方法。您还可以使用它来访问公共类变量。x
Foo
->
- Essentially the same thing as .
except this works on pointer types. In essence it dereferences the pointer, than calls .
. Using this is equivalent to (*ptr).method()
->
-.
除了这适用于指针类型之外,本质上是一样的。本质上,它取消引用指针,而不是调用.
。使用它相当于(*ptr).method()
回答by Platinum Azure
The three operators have related but different meanings, despite the misleading note from the IDE.
尽管来自 IDE 的误导性说明,这三个运算符具有相关但不同的含义。
The ::
operator is known as the scope resolution operator, and it is used to get from a namespace or class to one of its members.
该::
运算符称为范围解析运算符,用于从命名空间或类获取其成员之一。
The .
and ->
operators are for accessing an object instance's members, and only comes into play after creating an object instance. You use .
if you have an actual object (or a reference to the object, declared with &
in the declared type), and you use ->
if you have a pointer to an object (declared with *
in the declared type).
在.
与->
运营商的访问对象实例的成员,只有创建一个对象实例后进场。您可以使用.
,如果你有一个实际的对象(或参考对象,具有声明&
中声明的类型),并且使用->
,如果你有一个指向对象(声明*
在声明的类型)。
The this
object is always a pointer to the current instance, hence why the ->
operator is the only one that works.
该this
对象始终是指向当前实例的指针,因此->
操作符是唯一有效的操作符。
Examples:
例子:
// In a header file
namespace Namespace {
class Class {
private:
int x;
public:
Class() : x(4) {}
void incrementX();
};
}
// In an implementation file
namespace Namespace {
void Class::incrementX() { // Using scope resolution to get to the class member when we aren't using an instance
++(this->x); // this is a pointer, so using ->. Equivalent to ++((*this).x)
}
}
// In a separate file lies your main method
int main() {
Namespace::Class myInstance; // instantiates an instance. Note the scope resolution
Namespace::Class *myPointer = new Namespace::Class;
myInstance.incrementX(); // Calling a function on an object instance.
myPointer->incrementX(); // Calling a function on an object pointer.
(*myPointer).incrementX(); // Calling a function on an object pointer by dereferencing first
return 0;
}
回答by Jerry Coffin
You have a pointer to an object. Therefore, you need to access a field of an object that's pointed to by the pointer. To dereference the pointer you use *
, and to access a field, you use .
, so you can use:
你有一个指向对象的指针。因此,您需要访问指针指向的对象的字段。要取消引用您使用的指针*
并访问字段,请使用.
,因此您可以使用:
cout << (*kwadrat).val1;
Note that the parentheses are necessary. This operation is common enough that long ago (when C was young) they decided to create a "shorthand" method of doing it:
请注意,括号是必需的。这个操作很常见,很久以前(当 C 还年轻时)他们决定创建一个“速记”方法来做到这一点:
cout << kwadrat->val1;
These are defined to be identical. As you can see, the ->
basically just combines a *
and a .
into a single operation. If you were dealing directly with an object or a reference to an object, you'd be able to use the .
without dereferencing a pointer first:
这些被定义为相同。如您所见,->
基本上只是将 a*
和 a 组合.
成一个操作。如果您直接处理对象或对对象的引用,则可以使用.
而不先取消引用指针:
Kwadrat kwadrat2(2,3,4);
cout << kwadrat2.val1;
The ::
is the scope resolution operator. It is used when you only need to qualify the name, but you're not dealing with an individual object at all. This would be primarily to access a static data member:
该::
是范围解析操作。当您只需要限定name,但您根本不处理单个对象时使用它。这主要是为了访问静态数据成员:
struct something {
static int x; // this only declares `something::x`. Often found in a header
};
int something::x; // this defines `something::x`. Usually in .cpp/.cc/.C file.
In this case, since x
is static
, it's not associated with any particular instance of something
. In fact, it will exist even if no instance of that type of object has been created. In this case, we can access it with the scope resolution operator:
在这种情况下,由于x
is static
,它不与 的任何特定实例相关联something
。事实上,即使没有创建该类型对象的实例,它也会存在。在这种情况下,我们可以使用范围解析运算符访问它:
something::x = 10;
std::cout << something::x;
Note, however, that it's also permitted to access a static member as if it was a member of a particular object:
但是请注意,还允许访问静态成员,就好像它是特定对象的成员一样:
something s;
s.x = 1;
At least if memory serves, early in the history of C++ this wasn't allowed, but the meaning is unambiguous, so they decided to allow it.
至少如果没有记错的话,在 C++ 的早期历史中这是不允许的,但含义是明确的,所以他们决定允许它。
回答by Marcin Zaluski
The '::' is for static members.
'::' 用于静态成员。
回答by Gir
-> is for pointers to a class instance
-> 用于指向类实例的指针
. is for class instances
. 用于类实例
:: is for classnames - for example when using a static member
:: 用于类名 - 例如在使用静态成员时
回答by psynnott
Others have answered the different syntaxes, but please note, when you are doing your couts
, you are only using ->
:
其他人已经回答了不同的语法,但请注意,当你在做你的couts
,你只使用->
:
int main()
{
Kwadrat* kwadrat = new Kwadrat(1,2,3);
cout<<kwadrat->val1<<endl;
cout<<kwadrat->val2<<endl;
cout<<kwadrat->val3<<endl;
return 0;
}