개요
- if문 안에서 초기화 가능
- switch문도 가능
예제
- 코드
#include <iostream>
#include <type_traits>
using namespace std;
class Test {
private:
int i;
public:
Test(int i) : i(i) {}
bool Valid() {
cout << "Valid() : " << i << endl;
return true;
}
bool Run() {
cout << "Run() : " << i << endl;
return false;
}
int GetI() { return this->i; }
};
int main() {
if (Test t(0); t.Valid()) {
t.Run();
}
cout << "------" << endl;
switch (Test t(1); t.GetI()) {
case 0:
cout << "case 0" << endl;
break;
case 1:
cout << "case 1" << endl;
break;
default:
break;
}
return 0;
}
- 실행 결과
Valid() : 0
Run() : 0
------
case 1