Ojective-C @protocol equivalent in C++
Class A has an instance of class B as a member. Sometimes the instance of
class B wants to talk to class A. In Objective-C I can do:
// A.h
@interface A : NSObject <BDelegate>
@property (nonatomic, retain) B *b;
@end
// A.m
- (void) classBsays {
}
// B.h
@protocol BDelegate
- (void) classBsays;
@end
@interface B : NSObject
@property (nonatomic, assign) id<BDelegate> delegate;
@end
// B.m
@implementation B
- (void) f {
[delegate classBsays];
}
@end
I've done something similar in C++ using a void pointer on class B. But
this misses the part that says "class B's delegate should implement such
and such methods".
How can I imitate Objective-C's protocol in C++?
No comments:
Post a Comment