#include<iostream.h>
class MyClass
{
public:
MyClass(int a){X=a;}
void Print() const{cout<<"Const:X="<<X<<endl;}
void Print(){cout<<"X="<<X<<endl;}
private:
int X;
};
int main()
{
const MyClass my_const(10);
MyClass my(20);
my_const.Print();
my.Print();
}
本文通过一个C++程序示例展示了如何定义和使用类中的常量成员函数。程序中包含一个名为MyClass的类,该类拥有两种Print成员函数:一种为常量成员函数,另一种为非常量成员函数。主函数中创建了常量对象my_const和非常量对象my,并分别调用这两种Print函数来展示其不同。
1160

被折叠的 条评论
为什么被折叠?



