什么是obj-c委托模式的C ++等价物?

时间:2013-01-06 13:42:53

标签: c++ objective-c delegates

我非常熟悉obj-c,现在我正在尝试深入挖掘C ++。

我在寻找obj-c委托模式的C ++等价物。

3 个答案:

答案 0 :(得分:4)

您只需继承类(协议),而不是符合协议。 一个小例子:

class Delegate 
{
public:
// Some pure virtual method here.
virtual void method() = 0; 
};

class A : Delegate
{
   void method() { // Do something here... };
};

class B
{
   Delegate your_delegate;
   // Somewhere in your code you might need to call the method() using: your_delegate.method();
};

答案 1 :(得分:1)

实际上并不是1:1的等价物。 Objective C是一种有点动态类型的语言。 Obj-C中的protocol就像一个承诺,具有某些签名的函数('选择器')将在运行时存在。如果你不想,你不会 来实施它们,但是如果你不希望事情在运行时崩溃,你应该这样做,除非他们需要标记为@optional

比较C ++是100%静态类型。如果你说某个函数应该存在,那么存在,或者程序将无法编译。如果超类将函数声明为抽象,则子类必须继承它。多重继承(如果你想让一个类实现多个解耦协议,你需要它)是一个雷区。

静态类型(C ++)的优点是,您可以在编译时知道您的程序是否功能完整(是否存在应该有代码的每个地方的代码)。然而,这样做的代价是,有时你必须提出其他解决方案来填写Obj-C的协议和授权。

答案 2 :(得分:0)

“代表”一词仍然适用。没有语法模式,但您使用常用工具使用框架对其进行建模。

这里有很多代码,但界面非常干净。用户必须做的只是底部的一些代码。还有一些与构造函数相关的样板文件,但是这将在未来使用C ++ 11“继承构造函数”语法消失。

/* Class to be used as a member of the delegation host.
   Keeps a list of event clients, which you can iterate over.
   As a C++11 convenience, function call operator() is overloaded to call
   all clients with the given argument list.

   This is templated over the base type for the clients. The base type
   should define virtual function(s) for handling the events.
   Templating is necessary because we can't anticipate the number or types
   of these virtual functions. */

template< typename delegate >
struct delegator {
    typedef std::list< delegate * > list; // host interface
    list delegates;

    typedef typename list::iterator delegate_id; // client interface

    delegate_id add( delegate *d )
        { return delegates.insert( delegates.end(), d ); }

    void remove( delegate_id d )
        { delegates.erase( d ); }

#if __cplusplus >= 201103L // C++11-only convenient host interface
    template< typename ... args >
    void operator() ( args && ... a ) {
        for ( auto d : delegates ) ( *d )( std::forward< args >( a ) ... );
    }
#endif
};

/* Abstract base class for all delegate bases. Registers and unregisters
   from the delegator, but doesn't define any event handler. */
template< typename derived >
struct delegate {
    typedef ::delegator< derived > delegator;

    delegator &source;
    typename delegator::delegate_id id;

    delegate( delegator &in_source )
        : source( in_source ),
        id( source.add( static_cast< derived * >( this ) ) ) {}

    virtual ~delegate() { source.remove( id ); }
};

/* Example delegate base. Defines an event handler which clients must implement.
   Other types of events might declare other bases. */
struct my_delegate_base : delegate< my_delegate_base > {
    typedef delegate< my_delegate_base > base;
    typedef base::delegator delegator;
    my_delegate_base( delegator &d ) : base( d ) {}

    virtual void operator() ( int ) = 0;
};

/* Example client class defines how to handle an event. */
struct my_delegate_impl : my_delegate_base {
    my_delegate_impl( delegator &d ) : my_delegate_base( d ) {}

    virtual void operator() ( int i ) {
        std::cout << i << '\n';
    }
};

看到它运行:https://ideone.com/IRp5rJ