C++中修改由const修饰的私有属性
2015-07-18 21:12
471 查看
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; } }; int main(int argc, char **argv) { int *test3; A t; t.print(); test3 = &t.test2; test3--; *test3 = 40; t.print(); }
测试结果:
相关文章推荐
- C语言 - 结构体(struct)比特字段(:) 详细解释
- [C++ primer]虚函数和纯虚函数
- Leetcode-Implement Queue using Stacks(C++)
- C语言结构体、C++结构体和C++类的解析
- Leetcode-Implement Stack using Queues(C++)
- C++ 合并/去除字符串中多余的空格(创建新串)
- Leetcode-Min Stack(C++)
- LeetCode-Implement Trie (Prefix Tree) (C++)
- enum hack用法
- 实现一个函数,把一个字符串中的字符从小写转为大写。
- 栈变量被覆盖的问题在不同编译器中的表现,蛋疼的VC++
- c++ thread 笔记1
- C++学习笔记——面向对象
- C++中修改类的私有属性的方法
- VC++6.0 修改项目工程名称
- c++题目整理
- c++ primer读书笔记-第十五章 面向对象编程
- C++ 虚基类 派生与继承
- 黑马程序员-C语言基础知识-预处理
- 编写程序输出每种内置类型的长度