比方說,我們有如下的C++代碼:獲取協議,通過功能的對象參數
struct ISomeInterface
{
virtual ~ISomeInterface() {}
virtual void f() = 0;
};
class SomeClass : public ISomeInterface
{
public:
void f() override
{
std::cout << "Hi";
}
};
void getObject(ISomeInterface*& ptr)
{
ptr = new SomeClass;
}
int main()
{
ISomeInterface* p(nullptr);
getObject(p);
p->f();
delete p;
}
它不夠完美非常簡單而遠,但它作畫:獲得一個指向接口的對象通過函數的參數。
我們如何得到與Objective C協議相同的結果?
@protocol SomeProtocol <NSObject>
- (void)f;
@end
@interface SomeClass : NSObject<SomeProtocol>
- (void)f;
@end
@implementation SomeClass
- (void)f { NSLog(@"Hi"); }
@end
在此先感謝。
是的,實際上,它的工作。並沒有什麼特定的目標。謝謝 –