The this pointer is a pointer accessible only within the nonstatic 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. (come from MSDN)
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 callmyDate.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++ SpecificExample// Example of the this pointer void Date::setMonth( int mn ) { month = mn; // These three statements this->month = mn; // are equivalent (*this).month = mn; }
this 就是一个隐含参数,他指的就是对象本身,《深入浅出mfc》好像有详细讲解,只是书在家,不能帮你查了
平时我们 char* p = new char[10];这P是我们自己定义的,往后不用了,我们就delete p; p = NULL,也就没事了,可是这个 “白送”this的看不到摸不着,平是要用的话总有点不放心。还有他能在类中使用,而在不同类中有不同(指本身,不同类本身本该不同),那是否说它就是个局部指针了,即然是局部的它又是在什么时候指向他本身呢即是在什么时候初始化的呢?即然能指向本身那说明在指的那时,他(类)本身应该是完整的(构造好了,最起码有一部分是构造好了),那这个时刻到低又是在什么之前,什么之后呢?能否说清呢,还有this 到低是 void* 呢还是其它类型呢? 在程序执行过程中是否能给 this 赋个值呢?要是行的话,一个本该指向本身的指针一下子指向别人了,结果又如何呢?
(come from MSDN)
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 callmyDate.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++ SpecificExample// Example of the this pointer
void Date::setMonth( int mn )
{
month = mn; // These three statements
this->month = mn; // are equivalent
(*this).month = mn;
}
那我又想问,要是我在类的一个成员函数中执行“delete this”结果又会怎样呢,要是我又在析构函数中delete this,结果是不是一样呢?
等等,大家能否讨论下呢?
在程序执行过程中是否能给 this 赋个值呢?要是行的话,一个本该指向本身的指针一下子指向别人了,结果又如何呢?
产生类的一个对象时,系统会先为这个对象声请内存,然后调用构造函数对这个对象的数据区部分进行初始化。在构造函数被调用之前,对象应该已经存在,而this指针是在对象存在之后构造函数之前被定义,所以在构造函数中可以使用this指针,又因为对象未被初始化(构造函数未执行完),this指针的成员值是不确定的,所以此时访问this指针的成员的值或调用成员函数是危险的。
例如:
class A;
class B;class A
{
public:
B* pB;
int* pAny;
void Do1(B* p)
{
if (p->pAny != NULL) delete p->pAny;
}
void Do2(B* p)
{
pB = p;
}
};A a;class B
{
public:
int* pAny;
void Empty()
{
if (pAny != NULL) delete pAny;
}public:
B()
{
a.pB = this; // 这是安全的
a.Do1(this); // 不安全
a.Do2(this); // 这是安全的
a.pAny = this->pAny; // 这是安全的
*a.pAny = *this->pAny; // 不合理,*this->pAny的值是不确定的
this->Empty(); // 非法的,此时pAny并未申请内存,不应该调用delete pAny;
this->pAny = NULL; // 这是安全的
this->pAny = new int; // 这是安全的
}
};总的来说,在构造函数中使用this指针有3种情况发生:
1、是安全的
2、是不合理的
3、是非法的
类中的静态变量不含this指针,是所有类对象公用的。