Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@selector implementation in C++

I need to implement similar mechanism like @selector(Objecitive-C) in C++. By googling i have found. It does not help me...

template <class AnyClass> 
class ClassCallback
{
    // pointer to member function
    void (AnyClass::*function)(void);

    // pointer to object
    AnyClass object;  
public:

    ClassCallback()     {  }
    ~ClassCallback()    {  }

    ClassCallback(AnyClass _object, void(AnyClass::*_function)(void))
    : object( _object ), function( _function ) { };

    virtual void call(){ 
         (object->*function)();
    };

};

Is there any different approach to implement which is similar to objective-C like

typedef struct objc_selector    *MY_SEL;  

Need to implement objc_selector It is abstract class in objective-C. How to implement objc_selector any idea...

like image 816
Chandan Shetty SP Avatar asked Feb 25 '26 05:02

Chandan Shetty SP


2 Answers

Just a few precisions on the Objective-C messaging system, with the selectors.

A selector (SEL) is an opaque type, but it's only something like a char *, containing only the name of the method (so many classes can share the same selector).

An Obj-C method is a struct containing:

  1. A SEL field
  2. A char * containing infos about the return type and arguments.
  3. An IMP field

Something like:

struct objc_method
{
    SEL    method_name;
    char * method_types;
    IMP    method_imp;
};
typedef objc_method Method;

An IMP is just a C function pointer.

like image 87
Macmade Avatar answered Feb 27 '26 20:02

Macmade


Objective-C and C++ are very different languages, and as far as I know C++ doesn't have anything equivalent or even similar to Objective-C's selectors. If you explain the problem you're trying to solve, I'm sure someone here will be happy to discuss the way that problem might be dealt with from a C++ perspective.

like image 23
Caleb Avatar answered Feb 27 '26 19:02

Caleb