在C++ 中,this指针是一个特殊的指针,它指向当前对象的实例。 在C++ 中,每一个对象都能通过this指针来访问自己的地址。 this是一个隐藏的指针,可以在类的成员函数中使用,它可以用来指向调用对象。 当一个对象的成员函数被调用时,编译器会隐式地传递该对象的地址作为 this 指针。 友元函数没有this指针,因为友元不...
Type of 'this' pointer is "Class pointer" (or "Objet pointer") type - if there is a class named "Example" then, the type of 'this' pointer will be "Example*".Its type may depends on member function declaration in which the 'this' pointer is used. If the member function is ...
It is possible to executedelete this;, if the program can guarantee that the object was allocated bynew, however, this renders every pointer to the deallocated object invalid, including thethispointer itself: afterdelete this;returns, such member function cannot refer to a member of a class (...
Thethispointercanonlybecalledinamemberfunctionofa classthatrepresentstheaddressofthecurrentobject.Here's anexample: VoidDate::setMonth(int,Mn) { Month=Mn;//thethreesentencesareequivalent This->month=mn; (*this).Month=mn; } 1.thiscanonlybeusedinmemberfunctions. Globalfunctions,staticfunctionscannot...
As you would expect, this program produces the result: 2 Somehow, when we call simple.setID(2);, C++ knows that function setID() should operate on object simple, and that m_id actually refers to simple.m_id. The answer is that C++ utilizes a hidden pointer named this! In this les...
Jan 8, 2011 at 7:17am closed account (zb0S216C) Is it possible to delete a class member that allocates heap memory using the "this" pointer?. Take the following code segment:12345678910111213141516171819202122 class MY_CLASS { // Constructor. MY_CLASS( int nInit ) { // Allocate memory ...
error C2662, cannot convert ‘this’ pointer from ‘const class ’ to ‘class &’ 看一下导致这个编译错误的例子: class COwnInt { public: int get(); private: int m_n; }; int COwnInt::get() { return m_n; } int main() {
实际上,vbptr 指的是虚基类表指针(virtual base table pointer),该指针指向了一个虚基类表(virtual table),虚表中记录了虚基类与本类的偏移地址;通过偏移地址,这样就找到了虚基类成员,而虚继承也不用像普通多继承那样维持着公共基类(虚基类)的两份同样的拷贝,节省了存储空间。
Unit04-Section08-The Scope of Variables & this Pointer
实际上,vbptr 指的是虚基类表指针(virtual base table pointer),该指针指向了一个虚基类表(virtual table),虚表中记录了虚基类与本类的偏移地址;通过偏移地址,这样就找到了虚基类成员,而虚继承也不用像普通多继承那样维持着公共基类(虚基类)的两份同样的拷贝,节省了存储空间。