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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T18:01:21+00:00 2026-06-12T18:01:21+00:00

This is my first question on this platform. I am sorry if something is

  • 0

This is my first question on this platform. I am sorry if something is unclear or if I failed to ask in an appropriate way.

The code below should compile with any C++11 Compiler. I tried to reduce it to the minimum. The sense of this code might got lost during this procedure, but it should still be clear that I am trying to do keep a list of classes and corresponding member function to call them all with the same parameter:

#include <iostream>

class Base {
public:
  virtual void Call(int x) = 0;
};

template<class T> class Extended : public Base
{
  public:
  // Constructor.
  Extended(T* ptr, void (T::*memberFunction)(int)) : _ptr(ptr), _memberFunction(memberFunction) { }

  // Call function.
  void Call(int x) {
    (_ptr->*_memberFunction)(x);
  }

private:
  // Pointer to class T.
  T* _ptr;

  // Function pointer.
  void (T::*_memberFunction)(int);

};

class Test1 {
public:
  void Fun1(int x) { std::cout << "Hello " << x << std::endl; }
};

class Test2 {
public:
  void FunX(int x) { std::cout << (x * 2) << std::endl; }
};

class MyList {

 public:
 ~MyList() {
    for (auto it = _list.begin(); it != _list.end(); ++it) {
      delete (*it);
    }
  }

  template <class T> void Add(T* t, void (T::*memberFunction)(int)) {
    _list.push_back(new Extended<T>(t, memberFunction));
  }

  void CallAll(int g) {
    for (auto it = _list.begin(); it != _list.end(); ++it) {
        (*it)->Call(g);
    }
  }

private:
  std::list<Base*> _list;
};


int main() {
  MyList myList;
  Test1 test1;
  Test2 test2;
  myList.Add(&test1, &Test1::Fun1);
  myList.Add(&test2, &Test2::FunX);
  myList.CallAll(100);
}

This works perfect. My problem is that I do not know how to remove a class and member function from the list. Furthermore I do not want the same class and member function to be called twice which is virtually the same problem. I need to check two classes of type Base for equality. I could provide a virtual function which gives me a void pointer.

virtual void* GetPtr() = 0;

But that would only check for equality of the class. I have no clue how to check for equality of function pointers of this class and how

template <class T> void MyList::Remove(T* t, void (T::*memberFunction)(int)) {

}

has to look like.

Does someone know the solution to the problem? Or is this check not possible?

Joe

  • 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-06-12T18:01:22+00:00Added an answer on June 12, 2026 at 6:01 pm

    Add virtual isEqual method to Base.

    class Base {
    public:
      virtual void Call(int x) = 0;
      virtual bool isEqual(const Base& other) = 0;
    };
    
    template<class T> class Extended : public Base
    {
    
      public:
          virtual bool isEqual(const Base& other)
          {
             const Extended* otherPtr = dynamic_cast<const Extended*>(&other);
             return otherPtr != nullptr && otherPtr->_ptr == _ptr && otherPtr->_memberFunction == _memberFunction;
          }
    
    };
    

    And add virtual destructor to Base, otherwise you have memory leak,

    And do not use undescore at the beginning of member variable _ptr – if you must use at the end: ptr_. Some leading underscores are reserved for compiler.

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

Sidebar

Related Questions

this is my first question in here, and I would like to ask if
First of all, this is a follow-up question to Should I use WCF or
First and foremost, I'm so sorry about how long this question is and appreciate
This'll be my first question on this platform. I've done lots of development using
I ask for your patience about the lack of code snippets in this question
This is my first question on Stack Overflow, so I hope that I'm clear
This is my first question, so please be gentle. I'm trying out Apache Camel
this is my first question to SO so i'll try not to disgrace myself.
this is my first question I have searched for any launching activity questions and
this is my first question here so be gentle ! recently i try to

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.