shallowThought shallowThought - 2 months ago 10
Objective-C Question

What's the C++ equivalent of the obj-c delegate pattern?

Im pretty familiar with obj-c and now I´m trying to dig deeper in C++.

Im looking for a C++ equivalent for obj-c´s delegation pattern.

Answer

Instead of conforming to a protocol, you just inherit the class (protocol). A small example:

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();
};
Comments