现在的位置: 首页 > 综合 > 正文

在while的条件中定义变量

2013年10月02日 ⁄ 综合 ⁄ 共 271字 ⁄ 字号 评论关闭

《C++Primer》(中文第四版) P178:

“在循环条件中定义的变量在每次循环里都要经历创建和撤销的过程。”

原先没注意到这点,查了下C++标准,的确如此。测试程序:

#include <iostream>
using namespace std;

class A
{
public:
    A(int i):val(i){cout<<val<<endl;}
    operator bool(){return val!=10;}
    int val;
};

int main()
{
    int i=0;
    while(A a=i++);
    system("pause");
}

【上篇】
【下篇】

抱歉!评论已关闭.