您的位置:首页 > 编程语言 > C语言/C++

读书笔记 effective c++ Item 26 尽量推迟变量的定义

2017-03-03 19:17 597 查看

1. 定义变量会引发构造和析构开销

每当你定义一种类型的变量时:当控制流到达变量的定义点时,你引入了调用构造函数的开销,当离开变量的作用域之后,你引入了调用析构函数的开销。对未使用到的变量同样会产生开销,因此对这种定义要尽可能的避免。

2. 普通函数中的变量定义推迟

2.1 变量有可能不会被使用到的例子

你可能会想你永远不会定义未使用的变量,你可能要再考虑考虑。看下面的函数,此函数返回password的加密版本,提供的password需要足够长。如果password太短,函数会抛出一个logic_error类型的异常,此异常被定义在标准C++库中(Item 54):

// this function defines the variable "encrypted" too soon

std::string encryptPassword(const std::string& password)

{

using namespace std;

string encrypted;

if (password.length() < MinimumPasswordLength) {

throw logic_error("Password is too short");

}

... // do whatever is necessary to place an

// encrypted version of password in encrypted

return encrypted;

}


对象encrypted不是完全不会被用到,但是如果抛出了异常它就肯定不会被用到。这就是说,如果encryptPassword抛出了异常,你不会用到encrypted,但是你同样会为encrypted的构造函数和析构函数买单。因此,最好推迟encrypted的定义直到你认为你会使用它:

// this function postpones encrypted’s definition until it’s truly necessary

std::string encryptPassword(const std::string& password)

{

using namespace std;

if (password.length() < MinimumPasswordLength) {

throw logic_error("Password is too short");

}

string encrypted;

... // do whatever is necessary to place an

// encrypted version of password in encrypted

return encrypted;

}


2.2 推迟变量定义的一种方法

上面的代码看起来还是不够紧凑,因为encrypted定义时没有带任何初始化参数。也就意味着默认构造函数会被调用。在许多情况下,你对一个对象做的第一件事就是给它提供一些值,这通常通过赋值来进行。Item 4解释了为什么默认构造一个对象紧接着对其进行赋值要比用一个值对其初始化效率要低。其中的分析在这里同样适用。举个例子,假设encryptPassword函数的最困难的部分在下面的函数中执行:

void encrypt(std::string& s); // encrypts s in place


然后encryptPassword可以像下面这样实现,虽然这可能不是最好的方法:

// this function postpones encrypted’s definition until

// it’s necessary, but it’s still needlessly inefficient

std::string encryptPassword(const std::string& password)

{

... // import std and check length as above

string encrypted; // default-construct encrypted

encrypted = password; // assign to encrypted

encrypt(encrypted);

return encrypted;

}


2.2 推迟变量定义的更好方法

一个更好的方法是用password来初始化encypted,这样就跳过了无意义的和可能昂贵的默认构造函数:

// finally, the best way to define and initialize encrypted

std::string encryptPassword(const std::string& password)

{

... // import std and check length

string encrypted(password); // define and initialize via copy

// constructor

encrypt(encrypted);

return encrypted;

}


2.3 推迟变量定义的真正含义

这个建议是这个条款的标题中的“尽量推迟”的真正含义。你不但要将变量的定义推迟到你必须使用的时候,你同样应该尝试将定义推迟到你获得变量的初始化值的时候。这么做,你就能避免不必要的构造和析构,也避免了不必要的默认构造函数。并且,通过在意义已经明确的上下文中对变量进行初始化,你也帮助指明了使用此变量的意图

3. 如何处理循环中的变量定义

这时候你该想了:循环该怎么处理呢?如果一个变量只在一个循环中被使用,是将将变量定义在循环外,每次循环迭代为其赋值好呢?还是将其定义在循环内部好呢?也即是下面的结构哪个好?

// Approach A: define outside loop

Widget w;

for (int i = 0; i < n; ++i) {

w = some value dependent on i;

...

}

// Approach B: define inside loop

for (int i = 0; i < n; ++i) {

Widget w(some value dependent oni);

...

}


这里我用一个Widget类型的对象来替换string类型的对象,以避免对执行构造函数,析构函数或者赋值运算符的开销有任何偏见。

对于Widget来说,两种方法的开销如下:

方法一: 1个构造函数+1个析构函数+n个赋值运算

方法二:n个构造函数和n个析构函数

如果赋值运算的开销比一对构造函数/析构函数要小,方法A更加高效。尤其是在n很大的时候。否则,方法B要更高效。并且方法A比方法B使变量w在更大的范围内可见,这一点违反了程序的可理解性和可操作性。因此,除非你遇到下面两点:(1)赋值比构造/析构开销要小(2)你正在处理对性能敏感的代码。否则你应该默认使用方法B。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: