g*********e 发帖数: 42 | 1 class A
{
int i;
public:
A();
~A();
}
int main()
{
int i;
cin>>i;
switch(i)
{
case 1: A a1; break;
case 2: A a2; break;
}
return 0;
}
Thinking in C++ 上的例子,问题是编译会出错,因为case 1:有可能跳过
define a1; 为什么去掉case 2就可以编译了?不是一样可以跳过define a1? |
N**s 发帖数: 837 | 2 if u declare variables in switch statement, wrap the statement with {}
case 1: { A a1; } break;
case 2: { A a2; } break;
【在 g*********e 的大作中提到】 : class A : { : int i; : public: : A(); : ~A(); : } : int main() : { : int i;
|
g*********e 发帖数: 42 | 3 isee, thanks
发信人: Nets (网), 信区: Programming
标 题: Re: C++小问题
发信站: BBS 未名空间站 (Sat Jul 28 10:00:50 2007), 转信
if u declare variables in switch statement, wrap the statement with {}
case 1: { A a1; } break;
case 2: { A a2; } break; |
g*********e 发帖数: 42 | 4 终于搞清除了,就是一个跳过了对象的构造函数而没有跳过
对象的作用域的问题;
感觉这个thinking in c++好多地方讲的不明不白的,不知是
作者thinking完了写的,还是想这样写出来叫别人thinking的。 |
p****o 发帖数: 1340 | 5 in c, labels are really just labels -- nothing more, nothing less.
there is a very interesting construction called duff's device. your
question will become clear if you understand that case.
【在 g*********e 的大作中提到】 : class A : { : int i; : public: : A(); : ~A(); : } : int main() : { : int i;
|
d*******n 发帖数: 524 | 6 his problem is not a C problem.
【在 p****o 的大作中提到】 : in c, labels are really just labels -- nothing more, nothing less. : there is a very interesting construction called duff's device. your : question will become clear if you understand that case.
|