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;
}
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.