C++一个小例子赏析--关于常对象,常成员函数,常数据成员 const_cast

来源:互联网 发布:网络编程是做什么的 编辑:程序博客网 时间:2024/05/07 05:55

//在vx2005中编译通过

//希望这个例子对大家学C++语法有些启发或帮助

#include "stdafx.h"

#include <iostream>
using namespace std;
class ba //whether a const function can change a variables value
{
public:
ba():aa(0){ };
public:
void change() const
{
//const int* cur = &aa;
int& j=const_cast<int&>(aa);
j = 5;
};
const int aa;
float jj;
};
int main()
{

const ba mya;

cout<<mya.aa<<endl;                             ///输出结果 0

mya.change();

cout<<mya.aa<<endl;                             //输出结果5


int j =0;
cin>>j;
return 0;
}