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 6197381
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T03:48:38+00:00 2026-05-24T03:48:38+00:00

I posted a question earlier on how to cast a member function to a

  • 0

I posted a question earlier on how to cast a member function to a typedef function pointer ( How to use a typedef function pointer to register a callback ) and it prompted me to consider a slightly modified solution by implementing the “classic” observer pattern.

I want to allow class A to notify observers who get a callback via the function pointer and observers who get a callback via a member function, but I can’t figure out what’s const-correct way to do this. I’ve looked at other examples online and now I’m just confused with where I’m going wrong.

Here is an sscce compliant example that I pasted in ideone.com (with the compiler errors below it):

#include <set>

typedef int (*CallbackEvent)(const char*, const char*, int);

// Observer interface
class IObserver
{
public: 
    virtual ~IObserver(){}
    virtual int CallMeBack(const char*, const char*, int);
};

class A
{
private:
    std::set<IObserver> observers;
public:
    A(){}
    ~A(){}
    void RegisterObserver(const IObserver& observer)
    {
        observers.insert(observer);
    }

    inline void UnregisterObserver(const IObserver& observer)
    {
        observers.erase(observer);
    }

    void NotifyAll()
    {
        for(std::set<IObserver>::iterator itr = observers.begin(); itr != observers.end(); itr++)
        {
            int z = 3;
            itr->CallMeBack("x","y",z); // <-- This is the part that's not working
        }
    }
};

// Has internal logic to handle the callback
class B : public IObserver
{
private:
    A myA;
public:
    B(A& a)
    {
        myA = a;
        myA .RegisterObserver(*this);
    }
    ~B()
    {
        myA .UnregisterObserver(*this);
    }

    int CallMeBack(const char* x, const char* y, int z)
    {
        return 0;
    }
};

Compiler Errors:

prog.cpp: In member function ‘void A::NotifyAll()’:
prog.cpp:38: error: passing ‘const IObserver’ as ‘this’ argument of ‘virtual int IObserver::CallMeBack(const char*, const char*, int)’ discards qualifiers
/usr/lib/gcc/i686-pc-linux-gnu/4.3.4/include/g++-v4/bits/stl_function.h: In member function ‘bool std::less<_Tp>::operator()(const _Tp&, const _Tp&) const [with _Tp = IObserver]’:
/usr/lib/gcc/i686-pc-linux-gnu/4.3.4/include/g++-v4/bits/stl_tree.h:1141:   instantiated from ‘std::pair<typename std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator, bool> std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_insert_unique(const _Val&) [with _Key = IObserver, _Val = IObserver, _KeyOfValue = std::_Identity<IObserver>, _Compare = std::less<IObserver>, _Alloc = std::allocator<IObserver>]’
/usr/lib/gcc/i686-pc-linux-gnu/4.3.4/include/g++-v4/bits/stl_set.h:381:   instantiated from ‘std::pair<typename std::_Rb_tree<_Key, _Key, std::_Identity<_Key>, _Compare, typename _Alloc::rebind<_Key>::other>::const_iterator, bool> std::set<_Key, _Compare, _Alloc>::insert(const _Key&) [with _Key = IObserver, _Compare = std::less<IObserver>, _Alloc = std::allocator<IObserver>]’
prog.cpp:25:   instantiated from here
/usr/lib/gcc/i686-pc-linux-gnu/4.3.4/include/g++-v4/bits/stl_function.h:230: error: no match for ‘operator<’ in ‘__x < __y’

Please note that I do most of my programming in C#, so excuse my ignorance when it comes to the “nuances” of C++. Could anybody help me figure out what’s the right way to implement this?

Update

Changed my code based on Nawaz’s answer, here is the updated code: http://www.ideone.com/AH3KG

  • 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-24T03:48:39+00:00Added an answer on May 24, 2026 at 3:48 am

    The only thing that you might not be able to fix easily just by reading the error messages is:

    std::set<IObserver> observers;
    

    This needs to be a collection of pointers. Making copies of the observer objects is almost certainly the wrong thing to do. Use

    std::set<IObserver*> observers;
    

    This will also fix the problem with less_than, because pointers can be compared even when the objects they point to don’t define an ordering.

    Here it is, fixed: http://ideone.com/9jzpK

    Since you mentioned const-correctness, here’s a variant in which the observers are const: http://ideone.com/gxsCo

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I had posted a similar question earlier - a slightly different requirement here. I
I posted a question earlier about stopping all sounds in a swf. Now, to
I had posted one question earlier jQuery inconsistency in setting readonly attribute in IE-8
This is a follow-up to an earlier question I posted on EF4 entity keys
This is a related question to one I posted earlier... I'm trying to sum
I posted another question earlier about getting the arguments from a context menu App.
I ask because my buddy posted a question earlier on How to resolve issue
This is somewhat of a follow up to a question posted earlier last month.
I'm working from a question I posted earlier ( here ), and trying to
I posted a similar question earlier, but it was deleted because I felt I

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.