0

I'm working on a project that just needs to be rewritten but that is not an option at this point.

I have a C++ function that is called and does all kinds of stuff. I need it to read a variable from the App Delegate class.

For example I have:

@interface MyAppDelegate : NSObject <UIApplicationDelegate> 
{
    UIWindow *window;
    MyViewController *viewController;

 int mToleranceLevel;
}

I then have a function that needs to access the mToleranceLevel:

bool FindExtrinsics(...)
{
 float maxError = mainDelegate.mMaxError;
        ...
}

The problem is that this was declared like so:

@interface MyClass : UIViewController 
{
  ...
}

@properties ...

bool FindExtrinsics(...);

@end

So how would I get a value from the AppDelegate class. I do know how to get the current delegate:

mainDelegate = (RedStripeARAppDelegate *)[[UIApplication sharedApplication] delegate];

But how do I use this info to get the value in my C++ function. Is there a way to make a static variable so I can call MyAppDelegate.mToleranceValue;??

1
  • AppDelegate aD = ((AppDelegate)CCApplication::sharedApplication()); Commented Jul 9, 2014 at 7:46

2 Answers 2

2

Xcode supports Objective-C++, which enables you to use Objective-C calls from C++ code. Change the extension of your C++ code file from .cpp (or .cc) to .mm and you'll be able to get the value from your C++ code just as you would from Objective-C code.

Sign up to request clarification or add additional context in comments.

1 Comment

well I could NOT access from C++ function next variable: @property (nonatomic, weak) id<FrameProcessEvents> delegate;
0

Depending on the compiler and runtime, an Objective C object is usually just a pointer to a struct, and an instance variable may simply be a member of that C struct. And C syntax is a proper subset of Objective C. So your object variable access from C or C++ may be as simple as:

if (myObject != NULL) {
    x = myObject->myInstanceVariable;
}

1 Comment

Considering you need to include the Objective-C interface in your C++ code if you want this to work, you'll need Objective-C++ anyways, so you might want use the regular accessors as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.