C++中,this指针做为一个隐式参数传递给所有的非静态成员函数。this的类型取决于函数的声明。
例子1:如果类X的成员函数声明为const, 则this指针类型为const X*
class X
{
void fun() const
{
// this做为隐式参数传递给函数fun(). 指针类型为const X*
}
};
例子2:如果成员函数声明为volatile,则this指针类型为volatile X*
class X
{
void fun() volatile
{
//this做为隐式参数传递给函数fun(). 指针类型为volatile X*
}
};
例子3:如果成员函数声明为const volatile,则this指针类型为const volatile X*
class X
{
void fun() const volatile
{
//this做为隐式参数传递给函数fun(). 指针类型为const volatile X*
}
};
更多参考:
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1905.pdf