C++中修改类的私有属性的方法
2015-07-18 18:41
417 查看
C++中虽然可以直接修改私有属性的值,但不建议这样操作,此处只是为了告诉大家C++与C语言都是在做内存上面的工作!
代码如下:
运行结果:
代码如下:
#include <iostream> using std::cout; using std::endl; class A { private: int test1; public: int test2; public: A() { test1 = 10; test2 = 2; } void t() { cout << "This test1 is :" << test1 << endl; cout << "This test2 is :" << test2 << endl << endl; } }; int main(int argc, char **argv) { A t; int *a; t.t(); a = & t.test2; a --; *a = 30; t.t(); cout << "I love this test..." << endl; }
运行结果:
相关文章推荐
- VC++6.0 修改项目工程名称
- c++题目整理
- c++ primer读书笔记-第十五章 面向对象编程
- C++ 虚基类 派生与继承
- 黑马程序员-C语言基础知识-预处理
- 编写程序输出每种内置类型的长度
- C++中的链式操作
- C++中的链式操作
- 一起talk C栗子吧(第二十回:C语言实例--括号匹配)
- java的接口和C++虚类的相同和不同之处
- freopen - C/C++文件输入输出利器
- C语言中的格式字符 格式修饰符 运算符的优先级
- 2015年ALPC暑期专题练习I (计算几何) A
- 算法导论 第二十一章:不相交集合森林
- C++实现简单的对象池
- C语言程序设计9
- 作死向之C++告别演出——课程设计(银行储蓄系统(大BUG+功能不完整版))
- C语言程序设计7--8章
- C++ 使用STL string 实现的split,trim,replace-修订
- C++ 使用STL string 实现的split,trim,replace-修订