您的位置:首页 > 其它

条款25 :尽可能延后变量定义式的出现时间

2012-11-15 22:42 204 查看
只要你定义了一个变量,而类型带有一个构造函数和一个析构函数,你就得忍受构造成本与析构成本。或许你认为你不可能定义一个不会使用的变量,但是请看下面的代码:

std::stringencryptPassword(conststd::string&password){

usingnamespacestd;

stringencrypted;

if(password.length()<MinimumPasswordLength){

throwlogic_error("password is short");

}

...

returnencrypted;

}

对象encrypted并非完全没有被使用,但是当抛出异常时,这个对象确实没有被使用,但是你仍旧要忍受起encrypted对象的构造,析构成本。最好的方法是延后encrypted对象的定义式,直到确实需要它时。

std::stringencryptPassword(conststd::string&password){

usingnamespacestd;

if(password.length()<MinimumPasswordLength){

throwlogic_error("password is short");

}

stringencrypted; //延后encrypted对象的定义式

...

returnencrypted;

}

假设encryptPassword函数最艰难的部分在以下这个函数中进行:

voidencrypt(std::string&s);

则我们的encryptPassword函数可以这样实现:

std::stringencryptPassword(conststd::string&password){

usingnamespacestd;

...// 检查length,如前

stringencrypted;

encrypted=password;

encrypt(encrypted)

returnencrypted;

}

我们还可以接着去改进:我们知道上例中的encrypted是先定义,并调用了默认的构造函数,再用password进行赋值。这样,效率是比较低的,我们完全可以直接初始化。

std::stringencryptPassword(conststd::string&password){

usingnamespacestd;

...//

stringencrypted(password);

encrypt(encrypted)

returnencrypted;

}

在这里我们再次讨论“尽可能延后”的真正意义。你不只应该延后变量的定义,直到非得使用该变量前一刻为止,而是应该尝试延后这份定义直到能够给它初值实参为止。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: