Liny_@NotePad

沉迷ACG中

C++里的静态成员函数为何不能用const?

YOYO posted @ 2009年8月20日 01:05 in 【C/C++】 with tags static const this , 2383 阅读

细看const成员函数的定义:不会修改该对象的数据成员。
我们知道,访问成员函数时会自动带上this,形如CTest::SetColor(int color),会自动转换成CTest::SetColor(CTest* this, int color)。
在const成员函数时,实际转换成了CTest::SetColor(const CTest* this, int color)。
this指向的是一个const对象,const对象的数据成员是不能改变的。
而静态成员函数实际上是一个全局函数,没有this指针,根本不会访问到对象的数据成员,在此使用const就多此一举了。


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter