简体   繁体   中英

Objective C protocol reference as instance variable in C++ class

Isn't it possible to have a reference to an Objective C protocol inside a C++ class? The

(id)<B2ContactListener> _B2ContactListener;

line results in two errors:

' Expected ; at end of declaration list '

and

' C++ requires a type specifier for all declarations '.

Here's the code.

B2_ContactListener.mm:

#import "Box2D.h"
#import "B2_ContactListener.h"

class ContactListener : public b2ContactListener
{
private:
  (id)<B2ContactListener> _B2ContactListener; // ERRORs

public:
  //Methods
};

B2_ContactListener.h:

#import "B2_Contact.h"

@protocol B2ContactListener
-(void)B2BeginContact: (B2Contact*) contact;
@end

I'm using the current XCode compiler BTW.

Drop the parentheses:

id<B2ContactListener> _B2ContactListener;

That should work.

Another thing you may want to consider is making it an NSObject * instead, so you can invoke methods inherited from NSObject without having to cast.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM