const关键字
引用


也就是说 对于前一个式子,实际上时生成了一个临时常量temp=i,此时用r1引用了i
如果ri为非常量的话,显然程序员是为了将引用指向dval而非这个临时常量,因此这种操作是非法的

指针

只是不能通过cptr更改dval的值,但是可以通过其他方法(如直接更改dval)来更改dval的值
顶层const

如果允许改变对象的值,则该const是个底层const,否则是个顶层const
const成员函数

即以下两种情况
const Sales_data *p=this;//this指向的内容是可以改变的,因此不能赋给一个常量指针
const Sales_data ap;
ap.normalFuction(); //由于是const对象而该方法有可能改变对象的值,因此该调用是不允许的
using namespace std;
class A{
public:
A(){
cout<<"constructor A is called"<<endl;
}
A()const{
//不能声明构造函数的const 函数
}
void boo(){ //由于const函数只能被const对象调用,因此可以重载const函数
cout<<"boo is called"<<endl;
}
void boo() const{
cout<<"boo const is called"<<endl;
}
};
int main(){
const A a;
A b;
a.boo();
b.boo();
const A* c=&b;
c->boo();
}
using namespace std;
class A{
public:
A(){
cout<<"constructor A is called"<<endl;
}
void boo() const{
cout<<"boo const is called"<<endl;
}
};
int main(){
const A a;
A b;
a.boo();//此时会把this指针提升为一个常量指针常量
b.boo();
const A* c=&b;
c->boo();
}

