[英]concept and datatype of 'this' pointer in c++ and c#
I am a bit unclear about the concept of this pointer. 我对这个指针的概念有点不清楚。 I know the this pointer in c++ is hidden pointer and it is used to refer the current invoking object.
我知道c ++中的this指针是隐藏指针,它用于引用当前的调用对象。 But I want to know if there is a datatype for this pointer, For eg.
但是我想知道这个指针是否有数据类型,例如。 int *p;
int * p; states that p is a pointer to an integer.
说明p是指向整数的指针。 Similarly this pointer points to what?
同样这个指针指向什么? (I mean where is this 'this' pointer declared or written, or where does it exist and how is it written there.)
(我的意思是这个'this'指针在哪里声明或写入,或者它存在于何处以及如何在那里写入。)
Second question is in the context of C# The question is -- what is 'this' in the context of C# if it is not a pointer in C# (I found out it is not a pointer in C# when i tried using the -> arrow operator with 'this' keyword). 第二个问题是在C#的上下文中。问题是 - 如果它不是C#中的指针,那么在C#的上下文中是什么'(当我尝试使用 - >箭头时,我发现它不是C#中的指针运算符'this'关键字)。 And again, what is the datatype of 'this' in C# (I know the answer to the above question relating to c++ would answer it, but I wanted to know is it different in C# )
再说一次,C#中'this'的数据类型是什么(我知道上面关于c ++的问题的答案会回答它,但我想知道它在C#中是不同的)
PS : I am an amateur programmer, and in learning stage, so apologies for using terms like "how is it written there" instead of the proper technical terminologies in asking the question. PS:我是一名业余程序员,在学习阶段,很抱歉使用诸如“如何在那里写”之类的术语而不是在提出问题时使用适当的技术术语。
I want to know if there is a datatype for this pointer
我想知道这个指针是否有数据类型
The type of the hidden pointer this
is always the type of the class where the pointer is used. 类型的隐藏指针的
this
始终是在使用指针的类的类型。 For example, below 例如,下面
struct Foo {
void bar1() {
cout << this << endl;
}
void bar2() const {
cout << this << endl;
}
};
the type of this
inside bar1()
is Foo*
; 的类型
this
里面bar1()
是Foo*
; inside bar2() const
it is const Foo*
. 在
bar2() const
里面它是const Foo*
。
Similarly this pointer points to what?
同样这个指针指向什么?
It is a pointer to the current instance. 它是指向当前实例的指针。 For example, below
例如,下面
struct Foo {
void bar() {
cout << this << endl;
}
} foo;
the pointers this
inside bar()
and the expression &foo
point to the same object. 指针
this
内部bar()
和表达式&foo
指向同一个对象。
what is
this
in the context of C# if it is not a pointer?是什么
this
在C#的背景下,如果它不是一个指针?
C# has no pointers * , but its concept of object references is reasonably similar. C#没有指针* ,但它的对象引用概念相当类似。 In C#,
this
represents a reference to the current object. 在C#中,
this
表示对当前对象的引用。 The type of this
is the type of the class inside which it is referenced. 该类型的
this
里面它被引用的类的类型。
Note that the runtime type of the object pointed to by this
may be different - for example, it could be a subclass. 请注意,对象的运行时类型指向
this
可能是不同的-例如,它可能是一个子类。 However, the static type of the pointer this
matches the type of the class inside which the pointer this
is referenced. 然而,静态类型的指示器的
this
其内部指针的类的类型相匹配this
被引用。
* unless you venture into unsafe context, anyway. *无论如何,除非你冒险进入不安全的环境。
In C++ the this pointer is an implicit parameter passed to all nonstatic member functions (passed to them as a hidden argument). 在C ++中,this指针是一个传递给所有非静态成员函数的隐式参数(作为隐藏参数传递给它们)。 Have a look here for more explanations: http://www.tutorialspoint.com/cplusplus/cpp_this_pointer.htm and here: http://www.geeksforgeeks.org/this-pointer-in-c/ , http://msdn.microsoft.com/en-us/library/y0dddwwd.aspx .
看看这里更多的解释: http://www.tutorialspoint.com/cplusplus/cpp_this_pointer.htm这里: http://www.geeksforgeeks.org/this-pointer-in-c/ , HTTP:// MSDN .microsoft.com / zh-cn / library / y0dddwwd.aspx 。
In C# the "this" is a reserved keyword which is a reference to the object for which the method is running. 在C#中,“this”是一个保留关键字,它是对运行该方法的对象的引用。 As you know in C# you don't use pointers, but you use references to objects.
正如您在C#中所知,您不使用指针,但您使用对象的引用。
这里的恕我直言是一个有用的参考,它描述了问题很好: MSDN
Yes, this
pointer in C++ has a type. 是的,C ++中的
this
指针有一个类型。
class A
{
public:
void func() {}
}
Inside func
type of this
is:- 里面
func
类型的this
就是: -
A * const //for non-const functions.
A const * const //for const functions.
this
is most certainly a pointer in C++, in class Foo
it will be of the type Foo *
, unless you're in a volatile
or const
method - in that case those 2 modifiers will propagate to the this
type. this
肯定是C ++中的指针,在类Foo
它将是Foo *
类型,除非你处于volatile
或const
方法中 - 在这种情况下,这两个修饰符将传播到this
类型。
Similarly in C# this
is just a magical reference variable of the current object's type. 类似地,在C#中,
this
只是当前对象类型的神奇引用变量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.