开关盒标签内的变量声明 [英] variable declaration inside switch case label

查看:79
本文介绍了开关盒标签内的变量声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下程序:


#include< iostream>


使用命名空间std;


int main()

{

int i;


cin> i;


开关(i)

{

案例1:

int case_1 = 1;

cout<< case_1 << case_1<<结束;

休息;

案例2:

int case_2 = 2;

cout<< case_2 << case_2<<结束;

休息;

}


返回0;

}


如果在除最后一个案例

标签之外的任何案例标签内声明变量,我将收到编译错误。在这个程序中我得到

声明编译错误


int case_1 = 1;


内部案例1.


但声明没有编译错误


int case_2 = 2;


in case 2.


请说明有什么区别,为什么申报单不允许在其他案例标签中使用
但在最后一个案例标签中允许在一个

开关?

Consider the following program:

#include <iostream>

using namespace std;

int main()
{
int i;

cin >i;

switch ( i )
{
case 1:
int case_1 = 1;
cout << "case_1 " << case_1 << endl;
break;
case 2:
int case_2 = 2;
cout << "case_2 " << case_2 << endl;
break;
}

return 0;
}

If a variable is declared inside any case label except the last case
label, I am getting compilation error. In this program I am getting
compilation error for the declaration

int case_1 = 1;

inside case 1.

But there is no compilation error for the declaration

int case_2 = 2;

inside case 2.

Kindly explain what is the difference and why is the declaration not
allowed in other case labels but allowed in the last case label in a
switch ?

推荐答案

6月17日上午10:23,subramanian10。 .. @ yahoo.com,印度"

< subramanian10 ... @ yahoo.comwrote:

On Jun 17, 10:23 am, "subramanian10...@yahoo.com, India"
<subramanian10...@yahoo.comwrote:


如果a变量在任何case标签内声明,除了最后一个案例

标签,我收到编译错误。在这个程序中我得到

声明编译错误


int case_1 = 1;


内部案例1.


但声明没有编译错误


int case_2 = 2;


in case 2.


请说明有什么区别,为什么申报单不允许在其他案例标签中使用
但在最后一个案例标签中允许在一个

开关?
If a variable is declared inside any case label except the last case
label, I am getting compilation error. In this program I am getting
compilation error for the declaration

int case_1 = 1;

inside case 1.

But there is no compilation error for the declaration

int case_2 = 2;

inside case 2.

Kindly explain what is the difference and why is the declaration not
allowed in other case labels but allowed in the last case label in a
switch ?



自从我编程以来已经很长很长时间了,我正在开始

重新选择它,所以如果我错了,请耐心等待:


第一个案例陈述错误而不是

秒的事实让我相信它我没有报告case_2

上的错误,因为它在case_1之后不再处理,因为它已经错误地确定了

。您是否尝试删除int case_1 = 1以查看case_2 = 2行是否为
errs?

It''s been a long, long time since I programmed and I''m starting to
pick it back up, so bear with me if I''m wrong:

The fact that erroring out on the first case statement and not on the
second leads me to believe it''s not reporting the error on case_2
because it''s not processing anymore after case_1, because it''s already
in error. Have you tried removing the int case_1 = 1 to see if it
errs on the case_2 = 2 line?


ス************** @ yahoo.com ,印度写道:
su**************@yahoo.com, India wrote:

考虑以下程序:


#include< iostream>


使用命名空间std;


int main()

{

int i;


cin> i;


开关(i)

{

案例1:

int case_1 = 1;

cout<< case_1 << case_1<<结束;

休息;

案例2:

int case_2 = 2;

cout<< case_2 << case_2<<结束;

休息;

}


返回0;

}


如果在除最后一个案例

标签之外的任何案例标签内声明变量,我将收到编译错误。在这个程序中我得到

声明编译错误


int case_1 = 1;


内部案例1.


但声明没有编译错误


int case_2 = 2;


in case 2.


请说明有什么区别,为什么申报单不允许在其他案例标签中使用
但在最后一个案例标签中允许在一个

开关?
Consider the following program:

#include <iostream>

using namespace std;

int main()
{
int i;

cin >i;

switch ( i )
{
case 1:
int case_1 = 1;
cout << "case_1 " << case_1 << endl;
break;
case 2:
int case_2 = 2;
cout << "case_2 " << case_2 << endl;
break;
}

return 0;
}

If a variable is declared inside any case label except the last case
label, I am getting compilation error. In this program I am getting
compilation error for the declaration

int case_1 = 1;

inside case 1.

But there is no compilation error for the declaration

int case_2 = 2;

inside case 2.

Kindly explain what is the difference and why is the declaration not
allowed in other case labels but allowed in the last case label in a
switch ?



因为它是C ++的规则,跳转不能通过一个变量

声明在同一范围内。所以当你跳转到案例2时,你会在案例1中传递

变量声明。但是

最后一个案例中的变量声明是可以的,因为它从不跳过结果。


规则的原因是,如果你允许跳过一个变量

声明,那么编译器很难弄清楚是否

为该变量调用析构函数。如果你已经跳过了

变量声明,你就不需要调用析构函数,如果你没有跳过
那么你就可以了。


如果你想在switch语句中对decalre变量进行decalre,就这样做吧


switch(i)

{

案例1:

{

int case_1 = 1;

cout<< case_1 << case_1<<结束;

}

休息;

案例2:

{

int case_2 = 2;

cout<< case_2 << case_2<<结束;

}

休息;

}


额外的{和}意味着当

调用析构函数时编译器没有问题。


对于int变量没有ddestructors,但可能有

其他变量类型,并且决定使这个规则的所有变量都相同




john

Because it''s a rule of C++ that a jump cannot pass over a variable
declaration in the same scope. So when you jump to case 2, you pass over
the variable declaration in case 1. But the variable declaration in the
last case is OK, because it is never jumped over.

The reason for the rule is that if you allowed a jump over a variable
declaration it would be very hard for the compiler to work out whether
to call a destructor for that variable. If you had jumped over the
variable declaration you would not need to call the destructor, if you
had not jumped then you would.

If you want to decalre variable inside switch statements, do it like this

switch ( i )
{
case 1:
{
int case_1 = 1;
cout << "case_1 " << case_1 << endl;
}
break;
case 2:
{
int case_2 = 2;
cout << "case_2 " << case_2 << endl;
}
break;
}

The extra { and } mean that the compiler has no trouble working out when
to call destructors.

Of there are no ddestructors for int variables, but there could be for
other variable types, and it was decided to made all variables the same
for this rule.

john


6月17日上午10:56,John Harrison< john_androni ... @ hotmail.com>

写道:
On Jun 17, 10:56 am, John Harrison <john_androni...@hotmail.com>
wrote:

subramanian10 ... @ yahoo.com,India写道:
subramanian10...@yahoo.com, India wrote:

考虑以下程序:
Consider the following program:


#include< iostream>
#include <iostream>


using namespace std;
using namespace std;


int main()

{

int i;
int main()
{
int i;


cin> i;
cin >i;


switch(i)

{

case 1:

int case_1 = 1;

cout<< case_1 << case_1<<结束;

休息;

案例2:

int case_2 = 2;

cout<< case_2 << case_2<<结束;

休息;

}
switch ( i )
{
case 1:
int case_1 = 1;
cout << "case_1 " << case_1 << endl;
break;
case 2:
int case_2 = 2;
cout << "case_2 " << case_2 << endl;
break;
}


返回0;

}
return 0;
}


如果在任何案例标签内声明变量,除了最后一个案例

标签,我正在编译错误。在这个程序中,我得到

声明编译错误
If a variable is declared inside any case label except the last case
label, I am getting compilation error. In this program I am getting
compilation error for the declaration


int case_1 = 1;
int case_1 = 1;


in case 1.
inside case 1.


但是没有编译错误声明
But there is no compilation error for the declaration


int case_2 = 2;
int case_2 = 2;


in case 2.
inside case 2.


请说明有什么区别为什么声明不是

允许在其他案例标签中,但允许在

开关中的最后一个案例标签?
Kindly explain what is the difference and why is the declaration not
allowed in other case labels but allowed in the last case label in a
switch ?



因为这是一个C ++规则,跳转不能通过同一范围内的变量

声明。所以当你跳转到案例2时,你会在案例1中传递

变量声明。但是

最后一个案例中的变量声明是可以的,因为它从不跳过结果。


规则的原因是,如果你允许跳过一个变量

声明,那么编译器很难弄清楚是否

为该变量调用析构函数。如果你已经跳过了

变量声明,你就不需要调用析构函数,如果你没有跳过
那么你就可以了。


如果你想在switch语句中对decalre变量进行decalre,就这样做吧


switch(i)

{

案例1:

{

int case_1 = 1;

cout<< case_1 << case_1<<结束;

}

休息;

案例2:

{

int case_2 = 2;

cout<< case_2 << case_2<<结束;

}

休息;

}


额外的{和}意味着当

调用析构函数时编译器没有问题。


对于int变量没有ddestructors,但可能有

其他变量类型,并决定使此规则的所有变量相同




john


Because it''s a rule of C++ that a jump cannot pass over a variable
declaration in the same scope. So when you jump to case 2, you pass over
the variable declaration in case 1. But the variable declaration in the
last case is OK, because it is never jumped over.

The reason for the rule is that if you allowed a jump over a variable
declaration it would be very hard for the compiler to work out whether
to call a destructor for that variable. If you had jumped over the
variable declaration you would not need to call the destructor, if you
had not jumped then you would.

If you want to decalre variable inside switch statements, do it like this

switch ( i )
{
case 1:
{
int case_1 = 1;
cout << "case_1 " << case_1 << endl;
}
break;
case 2:
{
int case_2 = 2;
cout << "case_2 " << case_2 << endl;
}
break;
}

The extra { and } mean that the compiler has no trouble working out when
to call destructors.

Of there are no ddestructors for int variables, but there could be for
other variable types, and it was decided to made all variables the same
for this rule.

john



约翰 - 感谢您提供了很好的信息。这是在某处记录的吗?


Bharath

John - Thanks for good information. Is this documented somewhere?

Bharath


这篇关于开关盒标签内的变量声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆