tags:

views:

222

answers:

1

Why does

switch ([document currentTool]) {
 case DrawLine:
  NSBezierPath * testPath = [[NSBezierPath alloc]init];
        //...rest of code that uses testPath....

result in

error:syntax error before "*" token

for testPath?

+3  A: 

You can't instantiate an object inside a case statement unless you put it inside a new scope. This is because otherwise you could do something like this:

switch( ... ) {
    case A:
       MyClass obj( constructor stuff );
       // more stuff
       // fall through to next case
    case B:
       // what is the value of obj here? The constructor was never called
    ...
}

If you want the object to last for the duration of the case, you can do this:

switch( ... ) {
    case A: {
       MyClass obj( constructor stuff );
       // more stuff
       // fall through to next case
    }
    case B:
       // obj does not exist here
    ...
}

This is the same in Objective C as well as C and C++.

Graeme Perrow
Thanks Graeme! Shoulda figured that.
Joe