0

i am porting a game from Obj-C to C++. And stuck where I have to convert Obj-C protocol to equivalent in C++. I am confused what should be the right way to achieve same functionality in C++.

Please advise.

@protocol A <NSObject>

    -(void) B:(NSObject*)data;

@end


@interface callBack: NSObject
{
    id<A*> delegate;
}
user1908860
  • 509
  • 1
  • 9
  • 19

1 Answers1

5

You can achieve something similar to a protocol making a class with pure virtual methods. Conforming to that protocol will require to override such methods.

The "protocol" declaration

class ImageDelegate {
    public:
        virtual void getImage(UIImage *) = 0;
}

Then refer to ImageDelegate* as you would do with id<ImageDelegate>

class ResInfoCallback : public NSObject {
    public:
       ImageDelegate *delegate;
}

Finally you need a class implementing the "protocol", which means overriding the pure virtual methods of the "protocol" class.

class YourClassConformingToTheProtocol : public ImageDelegate {
    public:
        virtual void getImage(UIImage *image) {
            //do stuff
        }
}

As a final remark PLEASE use capitalized names for classes.

Gabriele Petronella
  • 106,943
  • 21
  • 217
  • 235