首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

关于const,该如何解决

2013-11-20 
关于const问下 void Manager::print() const {} 这里的const是什么意思?[解决办法]这叫函数签名。表示 成员

关于const
问下 

void Manager::print() const {}
 这里的const是什么意思?
[解决办法]

这叫函数签名。
表示 成员函数的 this 指针,所指对象是函数内部,不可以改变的。
就是把 this 指针 定义成这个样子:
const Manager * this const;
 对比一下:
1)void Manager::print() const {}的this 指针 
const Manager * this const;

2)void Manager::print() {}的this 指针 
 Manager * this const;
[解决办法]
等同于:

void Manager::print(){} const 

也就是说print()方法不能修改类的成员变量的值。
[解决办法]
类的成员函数后面加 const,
表明这个函数不会对这个类对象的数据成员(准确地说是非静态数据成员)作任何改变。 

热点排行