我编写了一个涉及使用switch语句的程序......但是在编译时它显示:
错误:跳转到案例标签.
为什么这样做?
#include <iostream>
#include <cstdlib>
#include <fstream>
#include <string>
using namespace std;
class contact
{
public:
string name;
int phonenumber;
string address;
contact() {
name= "Noname";
phonenumber= 0;
address= "Noaddress";
}
};
int main() {
contact *d;
d = new contact[200];
string name,add;
int choice,modchoice,t;//Variable for switch statement
int phno,phno1;
int i=0;
int initsize=0, i1=0;//i is declared as a static int variable
bool flag=false,flag_no_blank=false;
//TAKE DATA FROM FILES.....
//We create 3 files names, phone numbers, Address and then …Run Code Online (Sandbox Code Playgroud) 我目前正在研究一些相当古老的C++代码,并经常发现类似的东西
int i;
i = 42;
Run Code Online (Sandbox Code Playgroud)
要么
Object* someObject = NULL;
someObject = new Object();
Run Code Online (Sandbox Code Playgroud)
甚至
Object someObject;
someObject = getTheObject();
Run Code Online (Sandbox Code Playgroud)
我完全理解这段代码的作用,但我真的不知道变量定义和初始化的这种分离是否有用.我搜索了一些解释,但总是最终得到成员初始化列表或者应该定义局部变量的问题.
最后,我不明白为什么有人可能故意编写这段代码.它只是将定义和初始化分成两个后续行并创建开销 - 在最后一种情况下,它使用默认构造函数创建一个对象,仅在下一行中销毁它.
我想知道我是否应该简单地将代码更改为
int i = 42;
Object* someObject = new Object();
Object someObject = getTheObject();
Run Code Online (Sandbox Code Playgroud)
这会导致任何问题吗?
假设:
switch ( test ) {
// Set some variables, call some functions ?
int x = 1 ;
int y = function(x) ;
//
case 1 :
// Process for test = 1
...
break;
case 5 :
// Process for test = 5
...
break;
default :
// Process for all other cases.
...
}
Run Code Online (Sandbox Code Playgroud)
执行我在第一个之前添加的额外代码是"合法的" case吗?我在例子中从未见过这个.