const对象并不是什么都不可改变的

class A {
public:
    int x, y;
};
class B {
public:
    A *t;
    int *c, d;
};
void foo(const B& ob)
{
    //ob.t++; //不合法
    //ob.d++; //不合法
    ob.t->x++; //合法
    *(ob.c) = 3; //合法
}

在如上代码中const修饰的寓意相当于:
A *t 转变成 A* const t;
int *c 转变成 int* const c;
int d 转变成 const int d;
因此ob.t->x++是合法的
*(ob.c) = 3 是合法的


类似于下面的例子:
int a=5;
 int* const p = &a;
 *p = 6;

 

相关文章: