this 指针

this指针只能在一个类的成员函数中调用,它表示当前对象的地址。下面是一个例子:

void Date::setMonth( int mn )

{

month = mn; // 这三句是等价的

this->month = mn;

(*this).month = mn;

}

 

1. this只能在成员函数中使用。

全局函数,静态函数都不能使用this。

实际上,成员函数默认第一个参数为T* const register this。

如:

class A{public: int func(int p){}};

其中,func的原型在编译器看来应该是: int func(A* const register this, int p);

 

还看到另一种说法:

 1 void stop(bool is_blocking = false)
 2     {
 3         control = control & -2;
 4         do_write(GO, control);
 5         bool is_running = is_blocking;
 6         while(is_running)
 7         {
 8             is_running = this->is_running();
 9         }
10     }

这种说法与上面的程序相近,is_runing定义的既是一个bool型变量,也是一个函数。

 1 inline void do_write(int offset, int value)//内嵌函数
 2     {
 3 //        printf("0x%x + 0x%x:	 v:0x%x
", base_address, offset, value);
 4         IOWR(this->base_address, offset, value);
 5         if (debug_check_values) {
 6             alt_u8 check = (alt_u8)IORD(this->base_address, offset);
 7 //            printf("0x%x + 0x%x:	 v:0x%x	 c:0x%x
", base_address, offset, (alt_u8)value, check);
 8             // The naked IORD function pulls alt_u8 values out of memory, so we need to cast the value to alt_u8 to check assertion
 9             assert((alt_u8)value == check);
10         }
11     }
12 
13     inline void do_write(int base_address, int offset, int value)
14     {
15         IOWR(base_address, offset, value);
16     }

以上两个do_write函数也是一样的

原文地址:https://www.cnblogs.com/zhongguo135/p/6603867.html