Implementation of Observer Pattern in C++14 I have written a small class for implementing the observer pattern in C++(14). I would welcome any comments, corrections criticisms and suggestions.

Observable.h:

#include &lt;functional&gt;
#include &lt;vector&gt;

class Observable
{
public:
    using Callback = std::function&lt;void(void)&gt;;

    template&lt;typename O,typename F&gt;
    void AddObserver(O self, F f)
    {
        obs.push_back(Callback([self,f](){(self-&gt;*f)();}));
    }

    void NotifyObservers(){for(auto f: obs){f();}}

private:
    std::vector&lt;Callback&gt; obs{};
};


Sample Code

main.cpp

#include &lt;iostream&gt;    

#include "Observable.h"

class A : public Observable
{
    private:
        int a;
        std::string s;
};

class B : public Observable
{
public:
    void EventHappened()
    {
        std::cout &lt;&lt; "B has observed an event\n";
    }
};

void bar()
{
    std::cout &lt;&lt; "This will not work";
}

class C
{
public:
    explicit C(A&amp; a,B&amp; b) : m_a(a), m_b(b)
    {
        a.AddObserver(this,&amp;C::ReactToEventFromA);
        b.AddObserver(this,&amp;C::ReactToEventFromB);

        //a.AddObserver(this,bar); //This will Fail.
        //b.AddObserver(this,&amp;B::EventHappened); //This will Fail.
    }

    void ReactToEventFromA()
    {
        std::cout &lt;&lt; "C has observed the event from A\n";
    }

    void ReactToEventFromB()
    {
        std::cout &lt;&lt; "C has observed the event from B\n";
    }

private:
        A&amp; m_a;
        B&amp; m_b;
};


int main()
{
    A a;
    B b;
    C c(a,b);

    a.AddObserver(&amp;b,&amp;B::EventHappened);

    a.NotifyObservers();
    b.NotifyObservers();
}


The basic idea is that, to preserve simplicity, when you want a class to be able to notify observers, you just inherit publicly from Observable. And you use the AddObserver method to register any member function of any object to observe the class.

This is just a simple quick mock up, therefore the signature of the callback has been hardcoded to void(void). It shouldn't be too difficult to add the ability to register more complex functions. But this is it for now.
I'm not sure we really need a base Observable class to inherit from anymore, because with lambdas and function type erasure, this pattern has become more or less trivial.

But if you want to do it that way, I think you can simplify and generalize it a bit more, by relaxing constraints in addObserver's signature (which I renamed attach because it's the canonical name if I'm not mistaken):

#include &lt;iostream&gt;
#include &lt;functional&gt;

class Observable {
    using Callback = std::function&lt;void(void)&gt;;
    std::vector&lt;Callback&gt; notifications;
public:
    void attach(Callback c) { notifications.push_back(c); }
    void notify() { for (auto&amp; c : notifications) c(); }
};

struct Buzzer {
    void go() { std::cout &lt;&lt; "drrrr... drrrr...\n"; }
};

struct Rooster {
    void sing() { std::cout &lt;&lt; "Cocorico!\n"; }
};

struct Sun : public Observable {
    void rise() { std::cout &lt;&lt; "Waoooh! the sun's rising!\n"; notify(); }
};

int main() {
    Sun sun;
    Buzzer buzzer;
    Rooster rooster;

    sun.attach([&amp;buzzer] {buzzer.go();});
    sun.attach([&amp;rooster] {rooster.sing();});
    sun.rise();
}