Created
October 21, 2014 23:23
-
-
Save TSKGunGun/c445b333ca5105e3a884 to your computer and use it in GitHub Desktop.
ループ内変数について
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<string.h> | |
using namespace std; | |
void function(){ | |
//ループ内で変数を宣言する | |
for( int ii(0); ii < 0; ii++ ){ | |
string str = _T("IN") | |
} | |
/*スコープが狭い | |
コンストラクタ・デストラクタがループの数だけコールされる*/ | |
//ループの外で変数を宣言する | |
string str; | |
for( int ii(0); ii < 0; ii++ ){ | |
str = _T("OUT") | |
} | |
/* スコープが広い | |
コンストラクタ・デストラクタは1回だけ*/ | |
} |
元ネタ:Effective C++ 26項
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
ループの中・外で宣言するかによってスコープの大小は異なる。
個人的にはINTとかの組み込み型ならループ内で宣言、大きいクラス型ならループ外で宣言するのがベストなんじゃないかと。
ループ回数にもよるけど・・・・ループ回数が多いなら外でやるべき。