Objective-C @protocol等同于C ++ [英] Objective-C @protocol equivalent in C++
问题描述
// Ah
@interface A:NSObject< BDelegate>
@property(非原子,保留)B * b;
@end
// Am
- (void)classBsays {
}
// b
@protocol BDelegate
- (void)classBsays;
@end
@interface B:NSObject
@property(nonatomic,assign)id< BDelegate>代表;
@end
// B.m
@implementation B
- (void)f {
[delegate classBsays];
}
@end
我在C ++中使用在B类上的void指针。但是,这错过了B类代表应该实现这样的方法的部分。
如何在C ++中模仿Objective-C的协议?
您的示例的C ++等价物看起来像这样:
// A.hpp
#includeB.hpp
class A:public BDelegate {
public:
void classBSays(){}
B * b;
}
// B.hpp
class BDelegate {
public:
virtual void classBSays()= 0;
}
class B {
public:
void f(){delegate-> classBSays(); }
BDelegate * delegate;
}
请注意,我在这里使用了成员函数的内联实现,为了简洁 - 您可以在单独的 A中同样实现
和 A.classBSays()
和 Bf()
.cpp B.cpp
文件。
在这个例子中,类 BDelegate
是一个抽象基类(ABC),相当于您的 BDelegate
协议。通过仅包含纯粹的虚拟成员函数(前缀为关键字 virtual
和 = 0
后缀)的函数,它强制其子类提供了这些方法的实现,就像在Objective-C协议中使用 @required
标记(或没有标记)一样。事实上, BDelegate
只包含这样的功能是什么使它成为一个ABC。
您可以模拟Objective-C通过为ABC中的函数指定一个空体,这意味着子类不需要实现(因为它在ABC中实现) @optional
标签。例如,您可以通过如下修改 BDelegate
来模拟可选的 foo
方法:
@protocol BDelegate
- (void)classBsays;
@optional
- (void)foo;
@end
//大致相当于:
class BDelegate {
public:
virtual void classBSays()= 0;
virtual void foo(){}
}
使用该定义,类 A
可以根据需要选择是否提供 foo
的定义。请注意,这并不完全等同于Objective-C @optional
符号,因为 A
仍然会继承 BDelegate
的 foo
方法,如果它不提供自己的覆盖。另一方面,使用Objective-C协议, A
根本没有这样的方法,除非它明确地实现了它。
可以更全面地了解该主题。这里。
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++?
The C++ equivalent of your example looks something like this:
// A.hpp
#include "B.hpp"
class A : public BDelegate {
public:
void classBSays ( ) { }
B* b;
}
// B.hpp
class BDelegate {
public:
virtual void classBSays( ) = 0;
}
class B {
public:
void f ( ) { delegate->classBSays( ); }
BDelegate* delegate;
}
Note that I've used inline implementation of the member functions here, for brevity's sake - you could equally implement A.classBSays()
and B.f()
in separate A.cpp
and B.cpp
files if you wanted.
In this example, the class BDelegate
is an abstract base class (ABC), equivalent to your BDelegate
protocol. By containing only pure virtual member functions (functions preceded with the keyword virtual
and with the =0
suffix), it forces its subclasses to provide implementations for those methods, much as using a @required
tag (or no tag) does in an Objective-C protocol. The fact that BDelegate
contains only such functions is what makes it an ABC.
You can emulate the Objective-C @optional
tag by specifying an empty body for the function in your ABC, which means that subclasses are not required to implement it (since it is implemented in the ABC). For example, you could emulate an optional foo
method by modifying the BDelegate
as follows:
@protocol BDelegate
- (void) classBsays;
@optional
- (void) foo;
@end
// Is approximately equivalent to:
class BDelegate {
public:
virtual void classBSays( ) = 0;
virtual void foo( ) { }
}
Using that definition, the class A
could choose whether to provide a definition for foo
or not, as is desired. Note however that this is not exactly equivalent to the Objective-C @optional
notation, because A
will still inherit BDelegate
's foo
method if it doesn't provide it's own override. With the Objective-C protocol, on the other hand, A
would have no such method at all unless it explicitly implements it itself.
A more thorough introduction to the subject is available here.
这篇关于Objective-C @protocol等同于C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!