C++中修改由const修饰的私有属性

来源:互联网 发布:大数据与生活的联系 编辑:程序博客网 时间:2024/05/22 05:31

C++中虽然可以直接修改私有属性的值,但不建议这样操作,此处只是为了告诉大家C++与C语言都是在做内存上面的工作!
代码如下:

#include <iostream>using std::cout;using std::endl;class A{private:    const int test1;public:    int test2;    A():test1(10)    {        test2 = 20;    }    void print()    {        cout << "This is test1 :" << test1 << endl;        cout << "This is test2 :" << test2 << endl << endl;    }};intmain(int argc, char **argv){    int *test3;    A t;    t.print();    test3 = &t.test2;    test3--;    *test3 = 40;    t.print();}

测试结果:
这里写图片描述

0 0