Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

create an object in switch-case

i use visual studi 2008. (c++)

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

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,

a new method that's creates that object?

edit the code:

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

case '2' ....
like image 694
Tyzak Avatar asked Feb 14 '26 02:02

Tyzak


1 Answers

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;
}

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;
  }
}
like image 60
Peter Alexander Avatar answered Feb 16 '26 16:02

Peter Alexander



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!