Fredrik Johansson
Fredrik Johansson

Reputation: 1301

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.

Upvotes: 0

Views: 429

Answers (1)

Jacob Relkin
Jacob Relkin

Reputation: 163228

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.

Upvotes: 1

Related Questions