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 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其实是c++中的概念,意思是当前对象本身
很简单,你声明一个类的对象,在程序执行进入到类的成员函数后,this代表该对象所在的地址
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; }
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;
}
例如在一个dialog中this表示指向该对话框的指针,然后你可以使用this->来调用其中的成员变量、成员函数等。
一般来说用的不多。在对象内部直接可以用成员变量、成员函数的名字就OK。
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
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;
}
void CMyPage :: ShowPopDlg()
{
...
CPopDlg dlgPop(iType, this); //将自己(CMyPage对象)传给CPopDlg
...
}
如下:class classx{
private:int data;
int func1(int x); //等价于fun1(classx* this,int x)
public:
int func2(int y); //等价于fun2(classx* this,int y) classx* this 由编译系统给出
}
classx obj;
obj.fun2(y);等价于obj.fun2(this,y);
调用一个成员函数或成员数据需知道对象的地址和成员的偏移量
和this->SetWindowText("ggggggggggggg");并没有区别,就是对象本身的指针,一般用不用关系不大