这个指针 [英] this pointer
问题描述
有关此的一些问题指针:
1.要进行赋值功能,
Stribg& String :: operator =(const String& other)
{
...
返回* this;
}
我很难理解为什么* this匹配String&这里。可以
有人对此有所了解吗?
2.在静态成员函数中没有这个指针可以在里面访问
呢?
3.在const成员函数中是this。 const类型的指针
非const成员中的非const类型?
提前感谢!
>关于这个的一些问题指针:
1.制作赋值函数,
Stribg& String :: operator =(const String& other)
{
...
返回* this;
}
返回类型是String& (字符串引用)和赋值
运算符正在传递对自身的引用。分配完成。
我很难理解为什么* this匹配String&这里。
有人可以对此有所了解吗?
2.在静态成员函数中没有这个指针可以访问
里面吗?
静态成员函数中没有这个指针。
3.在const成员函数中是this。 const类型的指针,而非const成员中的非const类型?
是的,这个指针是const成员函数中的const指针。
提前致谢!
" stub" < ST ** @ asof.com>在消息新闻中写道:1qzob.22753
E ********** @ bgtnsc05-news.ops.worldnet.att.net ... < blockquote class =post_quotes>关于this的一些问题指针:
1.要进行赋值功能,
Stribg& String :: operator =(const String& other)
...
返回*这个;
}
我很难理解为什么* this匹配
String&这里。有人可以对此有所了解吗?
您在赋值运算符中返回* this,以便
可以执行以下操作:
A = b = C; // a,b,c是String'和
// b返回(* this)以便a可以分配
2.在静态成员函数中不是这个指针
可以进入吗?
静态方法没有这个指针,因为它们
与特定的对象实例无关。
HTH,
- J.
Some questions about "this" pointer:
1. To make an assignment function,
Stribg &String::operator=(const String &other)
{
...
return *this;
}
I have hard time to understand why "*this" matches a "String &" here. Could
someone shed some light on this?
2. In a static member function aren''t there "this" pointer accessible inside
it?
3. In a const member function is the "this" pointer of const type while
non-const type in a non-const member?
Thanks in advance!
> Some questions about "this" pointer:
1. To make an assignment function,
Stribg &String::operator=(const String &other)
{
...
return *this;
}
The return type is a String & (String reference) and the assignment
operator is passing back a reference to itself one the assignment is done.
I have hard time to understand why "*this" matches a "String &" here. Could someone shed some light on this?
2. In a static member function aren''t there "this" pointer accessible inside it?
No there is no this pointer in a static member function.
3. In a const member function is the "this" pointer of const type while
non-const type in a non-const member?
Yes the this pointer is a const pointer in a const member function.
Thanks in advance!
"stub" <st**@asof.com> wrote in message news:1qzob.22753
E**********@bgtnsc05-news.ops.worldnet.att.net...Some questions about "this" pointer:
1. To make an assignment function,
Stribg &String::operator=(const String &other)
{
...
return *this;
}
I have hard time to understand why "*this" matches a
"String &" here. Could someone shed some light on this?
You return *this in an assignment operator so that
it''s possible to do things like:
a=b=c; // a,b,c are String''s and
// b returns (*this) so that a can be assigned
2. In a static member function aren''t there "this" pointer
accessible inside it?
Static methods have no "this" pointer, because they
are not associated with a particular instance of an object.
HTH,
- J.
这篇关于这个指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!