Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6778593
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:17:03+00:00 2026-05-26T16:17:03+00:00

Using Observer pattern. I have a class, called Monitor for example, that is monitoring

  • 0

Using Observer pattern. I have a class, called Monitor for example, that is monitoring a collection of objects. The class is an Observer and each object in it’s collection is a Subject. Currently the collection is implemented as a std::list of shared_ptr. In the Update method of the Monitor class I want to check if the update is coming from one of the objects in it’s collection.

std::list<SomeSharedPointer> items_;
...
void Monitor::Update(Subject *subject)
{
    if(subject == something_)
    {
        DoSomething();
    }
    else if
    ??
    // if subject is one of the objects in our collection then do something..

}

Subject here is a raw pointer and my collection is a list of shared_ptr. How can I effectively check if the subject coming in is any one of the objects in my collection?

(Note my compiler, msvc, supports lambdas if there is an algorithmic solution requiring one)

UPDATE

I should add that I realize I can use a for loop over the container, but I’m wondering if there’s a snazzier way.

UPDATE 2

SomeSharedPointer is a typedef for std::shared_ptr<SomeType> where SomeType derives from abstract class Subject (standard Observer pattern implementation). SomeType will at some point call Notify() which will call the Update() method for each observer.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T16:17:03+00:00Added an answer on May 26, 2026 at 4:17 pm
    auto i = std::find_if(items_.begin(), items_.end(), 
        [=](const SomeSharedPointer& x) { return x.get() == subject; });
    
    if (i != c.end())
    { 
        // Object found, and i is an iterator pointing to it
    }
    

    A little helper method can make this more readable:

    typedef std::list<SomeSharedPtr> ObserverCollection;
    
    // You can also add a const version if needed
    ObserverCollection::iterator find_observer(Subject* s)
    {
        return std::find_if(items_.begin(), items_.end(), 
            [=](const SomeSharedPointer& x) { return x.get() == s; });
    }
    

    Then, you use it like this if you need the iterator

    auto i = find_observer(subject);
    if (i != items_.end())
    {
        // Object found
    }
    

    or simply like this if you don’t:

    if (find_observer(subject) != items_.end())
    {
        ...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a basic MVC pattern created in Java that uses the Observable/Observer class/interface.
I have a problem with Observer-Pattern and deadlock using threads. package observerDeadLock; import java.util.Observable;
I am trying to implement a simple Observer pattern using .net Observable class. I
I have observer.h , client.h and field.h files. In observer.h there is Subject class
I have used Observer Pattern for my application. I have a subject which has
I've noticed that my dependency injected, observer-pattern-heavy code (using Guava's EventBus ) is often
We have a class home work for design pattern class. In that we have
I have a project where I am using the observer pattern extensively for the
An Observer Design Pattern is the solution to loosely coupling objects so they can
In my model I have an array of objects called events. I would like

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.