条款26: 尽可能延后变量定义的出现时间
2015-09-06 15:19
218 查看
/*条款26: 尽可能延后变量定义的出现时间*/ #include<iostream> #include<string> #define MinimumPasswordLength 32 using namespace std; void encrypt(const string &s){ } string encryptPassword(const string &password){ // using namespace std; //string encrypted;//如果在这个位置定义 函数执行无论是否有异常抛出总是会有encryted的构造与析构的开销 if (password.length() < MinimumPasswordLength){ throw logic_error("Password is too short"); } //... /* string encrypted; 一次构造 encrypted = password; 一次拷贝赋值 encrypt(encrypted);*/ string encrypted(password);//一次构造并初始化 encrypt(encrypted); return encrypted; } class A{ public: A(int i){ } }; const int N = 10; int main(){ A a(1);// 一次构造 一次析构 for (int i = 0; i < N; ++i){ a = i;//某个值 n个赋值 //.... } for (int i = 0; i < N; ++i){ A b = i;//某个值 n个构造 n个析构 } //考虑 赋值成本低于一组构造与析构 第一个循环较优,否则第二个循环较优 system("pause"); return 0; }
相关文章推荐
- 文章标题
- Session待整理
- TCO 2015 Round 2A DIV1
- tar排除指定目录打包
- c++学习笔记-动态绑定
- 读取纯真IP数据库C++源代码
- Gradle: Running a jar file
- 中英文混合长度的计算以及截取
- iOS开发UI -- 在storeBoard中通过标识符找到页面
- 软件系统体系结构---C/S B/S P2P架构(概述)
- STL中的所有容器资料
- 积分图像的应用(二):非局部均值去噪(NL-means)
- Android 系统栏,状态栏的隐藏
- C语言putenv()函数和getenv()函数的使用详解
- android的m、mm、mmm编译命令的使用
- wampserver安装pthreads多线程扩展教程
- redhat 2.6 版本以上的内核编译
- 非局部均值去噪(NL-means)
- Swift UIButton UIButtonType
- 计算机算法常用术语中英对照