Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8517365
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T05:40:49+00:00 2026-06-11T05:40:49+00:00

I have ViewController in objective-c and most of my code is c++ (.mm). I’d

  • 0

I have ViewController in objective-c and most of my code is c++ (.mm). I’d like to setup some callbacks to member functions from obj-c (in c++) and call them from c++. Something like this (it’s very simplifyed):

@interface MyClass
{ }
-(void)my_callback;
@end

@implementation MyClass

-(void)my_callback
{
   printf("called!\n");
}

-(void)viewDidLoad
{
   // setup_callback( "to my_callback ?" );
}
@end

and:

void setup_callback(void(*func)()) { func(); }

this is not correct of course. Any advice how can I do it, please?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T05:40:51+00:00Added an answer on June 11, 2026 at 5:40 am

    You have a few options.

    Using blocks

    You may use blocks to convey your callback work. This is probably the simplest solution as it allows you to call your code without having to pass any parameter to the callback “function”. Blocks work in C and all its supersets with Clang, and Clang++ even allows implicit casts between blocks and lambdas.

    #include <dispatch/dispatch.h>
    
    void setup_callback(dispatch_block_t block)
    {
        // required to copy the block to the heap, otherwise it's on the stack
        dispatch_block_t copy = [block copy];
    
        // setup stuff here
        // when you want to call the callback, do as if it was a function pointer:
        // block();
    }
    
    int main()
    {
        MyClass* instance = [[MyClass alloc] init];
    
        setup_callback(^{
            [instance callback_method];
        });
    }
    

    That might require some reworking on the C++ end to accept functors (or just blocks if it’s simpler) instead of function pointers.

    Since blocks create closures, they’re very convenient for that kind of works.

    Blocks are an Apple extension to C, C++ and Objective-C. See more about them here.

    Use the Objective-C runtime to acquire the function pointer to the method you want to call

    Use the Objective-C runtime to access the function pointer of your selector. This is more tedious and requires you to keep track of three variables (the object to call the method on, the selector to use, and the method implementation), but it actually works even in the case you can’t use the Objective-C syntax.

    Objective-C method implementations are function pointers with this signature:

    typedef void (*IMP)(id self, SEL _cmd, ...);
    

    Where self is what you’d expect, _cmd is the selector that caused this method call (the _cmd variable is actually available in all Objective-C methods, try it), and the rest is considered variadic. You need to cast IMP variables into the proper function signature because the calling convention for variadic C functions doesn’t always match the calling convention for Objective-C method calls (the Objective-C method call is the standard function calling convention for your compiler, probably either cdecl or the amd64 calling convention, and the variadic calling convention is not always the same). A reinterpret_cast will be able to do it.

    Here’s some code I put together for similar intents. It uses C++11 variadic templates to help with getting the proper function signature.

    #include <objc/runtime.h>
    
    template<typename TReturnType, typename... TArguments>
    auto GetInstanceMethodPointer(Class class, SEL selector) -> TReturnType (*)(id, SEL, TArguments...)
    {
        Method m = class_getInstanceMethod(class, selector);
        IMP imp = method_getImplementation(m);
        return reinterpret_cast<TReturnType (*)(id, SEL, TArguments...)>(imp);
    }
    
    int main()
    {
        MyClass* instance = [[MyClass alloc] init];
        auto foo = GetInstanceMethodPointer<void>(
            [MyClass class],
            @selector(my_callback));
        // foo is a void (*)(id, SEL) function pointer
        foo(instance, @selector(my_callback));
    }
    

    Also take care that your instance is not nil before using the function call, because nil checking is handled by the Objective-C runtime. In this case, we’re bypassing it.

    Keep track of an object and a SEL

    Use -[NSObject performSelector:] to perform your callback. Basically a simpler version of the Objective-C runtime solution.

    void setup_callback(id object, SEL selector)
    {
        // do stuff
        // to execute the callback:
        // [object performSelector:selector];
    }
    
    int main()
    {
        MyClass* instance = [[MyClass alloc] init];
    
        setup_callback(instance, @selector(my_callback));
    }
    

    Wrapping your call inside a C++ function

    I think this one doesn’t really need any example. Create a function that accepts your object type as the first parameter and call the method you want on it. Similarly to the SEL solution, you then need to separately keep track of the function to call and the object on which to call it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For example I have a ViewController named firstViewController. Then I would like to call
I have a viewcontroller like the following. But the touchsBegan doestnt get detected. Can
I have a ViewController that has its own NIB. I would like to embed
i have more viewcontroller that start from rootviewcontroller. So for example i start with
I'm new to objective-c programming and I stuck. I have uislider in my viewcontroller
In several pieces of sample objective-c code I've seen people create new objects like
In my application i have some view controllers and one objective c class ,how
I have a ViewController that includes its own .nib file, I am wanting to
I have one ViewController, in this i have added one Custom UIView named as
I have a ViewController class called GamePlay. In GamePlay there is a nested class

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.