在 switch-case 中创建一个对象 [英] create an object in switch-case

查看:70
本文介绍了在 switch-case 中创建一个对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用visual studio 2008.(c++)

i use visual studi 2008. (c++)

在我的 switch 案例中,我想创建一个对象,但我不工作.

in my switch case a wanted to create an object, but i doens't work.

是不是我不能在 switch case 中创建对象?

is it right, that i can't create an object in a switch case?

如果这是对的,最好的解决方法是什么,

if that's right,whats the best way to work around it,

创建该对象的新方法?

编辑代码:

switch (causwahl){
case '1':
cAccount *oAccount = new cAccount (ID);

case '2' ....

推荐答案

对于这样一个模糊的问题,我不能肯定地说,但我猜你正在做这样的事情:

I can't say for sure with such a vague question, but I'm guessing that you're doing something like this:

switch(foo)
{
case 1:
  MyObject bar;
  // ...
  break;

case 2:
  MyObject bar;
  // ...
  break;
}

这是不允许的,因为每个 case 语句具有相同的作用域.如果要使用相同的变量名称,则需要提供更多范围:

This isn't allowed because each case statement has the same scope. You need to provide more scope if you want to use the same variable name:

switch(foo)
{
case 1:
  {
    MyObject bar;
    // ...
    break;
  }

case 2:
  {
    MyObject bar;
    // ...
    break;
  }
}

这篇关于在 switch-case 中创建一个对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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