const对象与const函数
来源:互联网 发布:淘宝全屏店招怎样制作 编辑:程序博客网 时间:2024/04/30 02:08
要指定const成员函数,我们只需在函数头后面附加const关键字即可。注意,我们只能对类成员函数这么做,对普通全局函数不能这么做。仅当某个函数是类成员时,将其声明为const才有意义,其作用是使该函数中的this指针成为const,这意味着我们不能在该函数的定义内在赋值语句左边写上类的数据成员——那将被编译器标记为错误。const成员函数不能调用同类的非const成员函数,因为那样也有可能修改当前对象。
当我们将某个对象声明为const之后,该对象可以调用的成员函数也都必须是const,否则程序将不能编译。
eg:
CPointSource &operator=(const CPointSource &point_source)
{
m_attenuation = point_source.m_attenuation;
m_lambda = point_source.m_lambda;
m_point = point_source.m_point;
m_wave = point_source.m_wave;
return *this;
}
注意: 只有常量静态整形值才能在类中进行初始化!!!
<script type="text/javascript"><!--google_ad_client = "pub-1307274723602242";/* 728x15, 创建于 09-3-6 */google_ad_slot = "3124229201";google_ad_width = 728;google_ad_height = 15;// --></script><script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"></script>
- const对象与const函数
- const成员函数与const对象
- const对象与const成员函数
- const成员函数与const对象
- const对象和const函数
- const对象的const函数
- const对象和const函数
- const对象const成员函数
- const对象和const函数
- c++中const对象与const成员函数
- C++杂记:const对象与const成员函数
- c++中const 对象与const 成员函数
- C++中const对象与const成员函数
- c++中 const 对象与 const 成员函数
- c++中 const 对象与 const 成员函数
- 【C++总结】函数形参,返回const对象与非const对象,成员函数,函数指针
- 对象与const
- const对象调用非const成员函数
- PlayCrafter.com(绝妙的一个flex游戏站点)
- java非递归生成树及遍历
- 老当益壮,我也开个博客。
- 更深层次分析一道试题——指针内存分配
- Juniper:企业数据通信的新力量。
- const对象与const函数
- JasperReport 用户手册
- 写给自己
- 通知图标的弹出菜单问题
- wine 1.1.2 发布了--值得关注
- 什么是虚拟IP地址
- java线程同步
- C# static 用法整理。。。
- CString.Format 用法