Creating a new object in a blocking block - why doesn't it work?

Why

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

will result in

error:syntax error before "*" token

for testPath?

+4
source share
1 answer

You cannot create an instance of an object inside a case statement unless you place it in a new area. 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 continue throughout the entire period, 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 in C and C ++.

+10
source

Source: https://habr.com/ru/post/912057/


All Articles