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

  • Home
  • SEARCH
  • 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 384679
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T15:24:14+00:00 2026-05-12T15:24:14+00:00

I have an abstract functor class that overloads operator() and derived objects that implement

  • 0

I have an abstract functor class that overloads operator() and derived objects that implement it.

I have a function (part of another class) that tries to take an Array of these functor classes and tries to pass a pointer to a member function to the std algorithm for_each(), here is a overview of what I’m doing:

EDIT: I have re-cleaned it and put the old small example for clarity.

class A{
  operator()(x param)=0;
  operator()(y param)=0;
}

class B: public A{
  operator()(x param); //implemented
  operator()(y param);
}
...// and other derived classes from A

void ClassXYZ::function(A** aArr, size_t aSize)
{
  ...//some code here

  for(size_t i = 0; i< aSize; i++){

    A* x = aArr[i];
    for(v.begin(), v.end(), ...//need to pass pointer/functor to right operator() of x here

..//other code
}

I’ve tried a few ways and I can’t figure out how to get it to work, I need to use the abstract type as I could have different derived types but they will all have to implement the same operator()(param x) function.

I just need the for_each() function to be able to call the member function operator()(param x). I have a different function where it has concrete implementations and simply passes an instance of those and it works. I’m trying to achieve a similar effect here but without the knowledge of what concrete classes I’m given.

What am I doing wrong?

  • 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-12T15:24:15+00:00Added an answer on May 12, 2026 at 3:24 pm

    If I understand what you want to do, there are quite a few errors in your code snippet:

    • sizeof aArr is wrong, you need to pass the size explicitly (noticed by ChrisW)
    • Missing virtual specifier on the original declaration of operator()()
    • Not sure where your for loop ends as there’s no matching } (I suspect it shouldn’t be there at all)

    Here’s some code that will loop through an array of A (or A-derived) objects and call operator() on each one, passing across a passed-in argument as the param parameter:

    #include <iostream>
    #include <algorithm>
    #include <functional>
    
    using namespace std;
    
    typedef double param;      // Just for concreteness
    
    class A {
    public:
        virtual void operator()(param x) = 0;
    };
    
    class B : public A {
    public:
        void operator()(param x) { cerr << "This is a B!  x==" << x << ".\n"; }
    };
    
    void function(A** aArr, size_t n, param theParam) {
        void (A::*sFunc)(param x) = &A::operator();
        for_each(aArr, aArr + n, bind2nd(mem_fun(sFunc), theParam));
    }
    
    int main(int argc, char** argv) {
        A* arr[] = { new B(), new B(), new B() };
    
        function(arr, 3, 42.69);
    
        delete arr[0];
        delete arr[1];
        delete arr[2];
        return 0;
    }
    

    mem_fun() is necessary to convert a 1-parameter member function pointer to a 2-parameter function object; bind2nd() then produces from that a 1-parameter function object that fixes the argument supplied to function() as the 2nd argument. (for_each() requires a 1-parameter function pointer or function object.)

    EDIT: Based on Alex Tingle’s answer, I infer that you might have wanted function() to do many things on a single A-derived object. In that case, you’ll want something like:

    void function(A** aArr, size_t n, vector<param> const& params) {
        for (size_t i = 0; i < n; ++i) {
            void (A::*sFunc)(param x) = &A::operator();
            for_each(params.begin(), params.end(), bind1st(mem_fun(sFunc), aArr[i]));
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 193k
  • Answers 193k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer At my last company, we deployed both the commander and… May 12, 2026 at 6:39 pm
  • Editorial Team
    Editorial Team added an answer No. And due to security concerns, flex doesn't let you… May 12, 2026 at 6:39 pm
  • Editorial Team
    Editorial Team added an answer Always troubleshoot by looking at the SQL string -- not… May 12, 2026 at 6:39 pm

Related Questions

I'm trying to use stl algorithm for_each without proliferating templates throughout my code. std::for_each
I have an abstract Class Monitor.java which is subclassed by a Class EmailMonitor.java .
I have an abstract class and I would like to use it quickly by
I have an abstract generic class BLL<T> where T : BusinessObject . I need
I have an abstract base class which acts as an interface. I have two

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.