c++ const

来源:互联网 发布:淘宝助理导入宝贝详情 编辑:程序博客网 时间:2024/06/08 06:17

1、一般一来说,把一个const值赋给一个非const值是可以,如:

const int a = 10;

int b = a;

但是指针例外,如:

const int* a = new int;

int* b = a;//error

可以理解,因为通过指针可以改变内容。

 

2、class 函数体的const中使用this指针的话,this指针为const this

 

如:

 

 

 

3、 <<重载

ostream& <<operator(ostream& o,con c) //两处的引用均为必须,因为没有引用会调用拷贝构造函数,而其在ostream是私有的。

{

  return o<<c.a;

}