指针和引用
2015-10-23 12:35
323 查看
// test.cpp : 自定义控制台应用程序的入口点。 // #include "stdafx.h" #include <iostream> #include <vector> using namespace std; struct A { public: int num; }; std::vector<A> nums; bool Modify1(A& a) { // a = b; a = nums[0]; return true; } bool Modify2(A*& ptr) { ptr = &nums[0]; return true; } int main() { // 模拟stack. A b; b.num = 1987; nums.push_back(b); // 你如今的实现方法. A a; Modify1(a); A* aptr = &a; aptr->num = 20; cout<< nums[0].num << endl; // 更改后的实现方法 A* aa; Modify2(aa); aa->num = 17; cout<< nums[0].num << endl; return 0; }
执行结水果:
1987
17
相关文章推荐
- Wiggle Sort 解答
- Java简单获取字符串像素的方法
- Redis学习笔记三、List数据类型
- 基本字符串压缩
- srpingmvc-restful风格学习总结
- 无勇无谋
- django 自定义tag和filter
- [贪心入门]完美字符串
- unity3d抛物线的脚本
- Homestead 使用总结
- Android 访问内部存储文件
- hdu 1754 i hate it
- 触发器实例
- Redis学习笔记二、String数据类型
- 关于使用微信登录第三方APP的实现(Android版)
- Struts2 和 Struts1.x 的全面比较
- java 中 Cannot make a static reference to the non-static 解决方法
- HDU2639Bone Collector II(01背包变形)
- 个人使用angular的一些小总结
- 经典的lcs问题(输出公共子序列)