大佬教程收集整理的这篇文章主要介绍了C++常量成员函数,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
class ConstExample { int x; public: ConstExample(int a){ x = a;} void SETVALue(int); int getValue() const; };getValue 函数定义语句应如下:
int ConstExample::getValue() const { return x; }具有常量形参 X 的函数无法转向,并将 X 作为非常量形参传递给另一个函数。换句话说,承诺不修改 X 的函数不能将X传递给另一个函数,除非第二个函数也承诺不修改 X。这种情况有时也可能会以不明显的方式发生。
#include <iostream> using namespace std; class K { public: void output () // 丢矢了 const { cout << "Output of a K object" << endl; } }; void outputFirst(const K arr[]) { arr[0] .output (); } int main(int argc,char** argv) { K arr [] = { K() }; outputFirst(arr); return 0; }该程序之所以不能编译,是因为编译器不能保证 const 数组的元素在作为隐含的 this 形参传递给 output 成员函数时不会被修改:
class K { public: void output() const { cout << "Output of a K object" << endl; } };
以上是大佬教程为你收集整理的C++常量成员函数全部内容,希望文章能够帮你解决C++常量成员函数所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。