title: Good way to create observer design pattern in C++
body: <p>I am trying to implement observer design pattern in C++ as below</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;vector&gt;
  
  using namespace std;
  
  class observer
  {
      public:
          observer() = default;
          ~observer() = default;
  
          virtual void notify() = 0;
  };
  
  class subject
  {
      vector &lt;observer *&gt; vec;
  
      public:
          subject() = default;
          ~subject() = default;
  
          void _register(observer *obj)
          {
              vec.push_back(obj);
          }
  
          void unregister(observer *obj)
          {
              int i;
              for(i = 0; i &lt; vec.size(); i++)
              {
                  if(vec[i] == obj)
                  {
                      cout &lt;&lt; "found elem. unregistering" &lt;&lt; endl;
                      vec.erase(vec.begin() + i);
                      break;
                  }
              }
  
              if(i == vec.size())
              {
                  cout &lt;&lt; "elem not found to unregister" &lt;&lt; endl;
              }
          }
  
          void notify()
          {
              vector &lt;observer *&gt;::iterator it = vec.begin();
              while(it != vec.end())
              {
                  (*it)-&gt;notify();
                  it ++;
              }
          }
  };
  
  class obsone : public observer
  {
      void notify()
      {
          cout &lt;&lt; "in obsone notify" &lt;&lt; endl;
      }
  };
  
  class obstwo : public observer
  {
      void notify()
      {
          cout &lt;&lt; "in obstwo notify" &lt;&lt; endl;
      }
  };
  
  int main()
  {
      subject sub;
  
      obsone *one = new obsone();
      obstwo *two = new obstwo();
  
      sub._register(one);   
      sub._register(two);
      sub.notify();
  
      sub.unregister(one);
      sub.notify();
  
      //delete two;
      //sub.notify();
  
      return 0;
  }
  </code></pre>
  
  <p>I am registering the objects with the subject explicitly. Is it the correct way of doing it or do I need to register through observer class only. Are there any problems with the above approach?</p>