请问什么是this指针?

samueler 2001-07-04 02:41:25
请各位高手详细解释一下this指针?以及它在平时编程中所起到的作用(最好举例说明)
...全文
291 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
samueler 2001-07-05
  • 打赏
  • 举报
回复
谢谢大家,我已经明白了很多
qian 2001-07-05
  • 打赏
  • 举报
回复
up
chinesealbert 2001-07-05
  • 打赏
  • 举报
回复
this 指针就是自己,例如,任何一个方法都是在一个对象中,而如果想调用一个方法,而这个方法又是属于自己(在这里,自己是指一个对象),那么在不方便写出自己(也就是对象)时,就可以用this来代替自己。一般,this使用在定义自己这个对象的内部
prog_st 2001-07-05
  • 打赏
  • 举报
回复
this
C++ Specific —>

The this pointer is a pointer accessible only within the member functions of a class, struct, or union type. It points to the object for which the member function is called. Static member functions do not have a this pointer.

When a nonstatic member function is called for an object, the address of the object is passed as a hidden argument to the function. For example, the following function call

myDate.setMonth( 3 );
can be interpreted this way:

setMonth( &myDate, 3 );
The object's address is available from within the member function as the this pointer. It is legal, though unnecessary, to use the this pointer when referring to members of the class.

The expression (*this) is commonly used to return the current object from a member function.

Note Modifying the this pointer is illegal in the latest version of C++.

END C++ Specific

Example

// Example of the this pointer
void Date::setMonth( int mn )
{
month = mn; // These three statements
this->month = mn; // are equivalent
(*this).month = mn;
}
xiaolizi 2001-07-04
  • 打赏
  • 举报
回复

小李子觉得,如果没有this, 应该就不会有c++
liu_feng_fly 2001-07-04
  • 打赏
  • 举报
回复
在调用函数(不是静态的函数)的时候,要多传一个this指针,因为函数在内存中只有一份,就是用this来区分到底是那个类的实例(可以有多个)调用这个函数
cpunion 2001-07-04
  • 打赏
  • 举报
回复
可以传送自己的地址
作用大着呢

你看在VC生成的框架窗口中经常用到
zhourongbiao 2001-07-04
  • 打赏
  • 举报
回复
this表示自己呀。

class e{
int i;
void Fun(int i);//为e类的成员i赋值为参数i的值
}

void Fun(int i)
{
this.i = i;//这样可以区别不同的i
}

wjyasd 2001-07-04
  • 打赏
  • 举报
回复
就是就是呀!
longwx 2001-07-04
  • 打赏
  • 举报
回复
所有讲C++的书都有讲嘛,你要详细到什么地步?
z_sky 2001-07-04
  • 打赏
  • 举报
回复
你问的这么..., 高手们都不好意思回答。我不是高手,就我来说说。其实认真看书就好了。
this是在对象的成员函数里指向对象自己的指针,比如:
class A {
void funcA();
}
void pub_func(class *a) {..}

class A::funcA() {
pub_func(this);
}

16,551

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Creator Browser
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧