Omkar Somani
Omkar Somani

Reputation: 101

Why can’t variables be declared in a switch statement?

I want to know more about "Why can’t variables be declared in a switch statement?"

I read the post but i am not getting it exactly. You can just declare variable inside switch but to decalre and initialize a variable or to declare object of class it gives complie time error.

Please explain me....

Upvotes: 8

Views: 1862

Answers (2)

Gorpik
Gorpik

Reputation: 11028

There is a conflict here between language syntax and common sense. For us humans, it looks like this code (taken from 1800 INFORMATION's answer) should work fine:

class A
{
  // has some non-trivial constructor and destructor
};

switch (x)
{
  case 1:
    A a;
    break;
  default:
    // do something else
}

After all, the curly braces define the scope for a; it is only created if we enter case 1, it is destroyed immediately after leaving the case 1 block and it will never be used unless we enter case 1. In fact, the case labels and the break instruction do not separate scopes, so a exists in all the block afterwards, even though it is logically unreachable. And sure, there is no such thing as a case 1 block from a syntactic point of view.

If you think of the switch statement as a bunch of (structured) goto instructions in disguise, the scope problem becomes more evident:

{
  if (x == 1)
    goto 1;
  else
    goto default;

  1:
    A a;
    goto end;

  default:
    // do something else

  end:
}

Upvotes: 8

1800 INFORMATION
1800 INFORMATION

Reputation: 135245

Essentially because the initialisation of the variable would be skipped if the label containing the variable initialisation was not hit. This would be bad because the compiler would then have to emit code that would destroy said variable if and only if the initialisation code had run.

For example:

class A
{
  // has some non-trivial constructor and destructor
};

switch (x)
{
  case 1:
    A a;
    break;
  default:
    // do something else
}

If the code had hit the default, then a would not have been initialised. The compiler would have to have been able to figure this out in advance. Probably for performance reasons, this was disallowed.

The simple fix is to introduce a new layer of scope:

class A
{
  // has some non-trivial constructor and destructor
};

switch (x)
{
  case 1:
    {
      A a;
    }
    break;
  default:
    // do something else
}

This makes it ok, the destruction of a is now well defined.

Upvotes: 20

Related Questions