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?
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++.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With