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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T02:58:10+00:00 2026-05-11T02:58:10+00:00

I have a class that has a vector of another class objects as a

  • 0

I have a class that has a vector of another class objects as a member. In many functions of this class I have to do same operation on all the objects in the vector:

class Small {   public:     void foo();      void bar(int x);     // and many more functions };  class Big {   public:     void foo()     {         for (size_t i = 0; i <  VectorOfSmalls.size(); i++)             VectorOfSmalls[i]->foo();     }     void bar(int x)     {         for (size_t i = 0; i <  VectorOfSmalls.size(); i++)             VectorOfSmalls[i]->bar(x);     }     // and many more functions   private:     vector<Small*> VectorOfSmalls; }; 

I want to simplify the code, and find a way not to duplicate going other the vector in every function.

I’ve considered creating a function that receives a pointer to function, and calls the pointed function on every member of a vector. But I am not sure that using pointers to functions in C++ is a good idea.

I have also been thinking about functors and functionoids, but it will force me to create a class per each function and it sounds like an overkill.

Another possible solution is creating a function that receives a string, and calls the command according to the string:

void Big::call_command(const string & command) {     for (size_t i = 0; i <  VectorOfSmalls.size(); i++)     {        if (command == 'foo')            VectorOfSmalls[i]->foo();        else if (command == 'bar')            VectorOfSmalls[i]->bar();     } } void Big::foo() {     call_command('foo'); } 

But it might work slow (unneeded creation of a string instead of just a function call), and also creates a problem if functions have different signature.

So what would you recommend? Should I leave everything the same as it is now?

EDIT: I can use only STL and not boost (old compilers).

  • 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. 2026-05-11T02:58:10+00:00Added an answer on May 11, 2026 at 2:58 am

    Well you can rewrite the for loops to use iterators and more of the STL like this:

    void foo() {     std::for_each(VectorOfSmalls.begin(), VectorOfSmalls.end(), std::mem_fun(&Small::foo)); }  void bar() {     std::for_each(VectorOfSmalls.begin(), VectorOfSmalls.end(), std::mem_fun(&Small::bar)); } 

    beyond that, you could use some macros to avoid retyping that a lot, but I’m not a huge fan of that. Personally, I like the multiple functions over the single one which takes a command string. As it gives you more versatility over how the decision is made.

    If you do go with a single function taking a param to decide which to do, I would use an enum and a switch like this, it would be more efficient than strings and a cascading if. Also, in your example you have the if to decide which to do inside the loop. It is more efficient to check outside the loop and have redundant copies of the loop since ‘which command’ only needs to be decided once per call. (NOTE: you can make the command a template parameter if it is known at compile time, which it sounds like it is).

    class Big { public:     enum Command {         DO_FOO,         DO_BAR     };  void doit(Command cmd) {     switch(cmd) {     case DO_FOO:         std::for_each(VectorOfSmalls.begin(), VectorOfSmalls.end(), std::mem_fun(&Small::foo));         break;     case DO_BAR:         std::for_each(VectorOfSmalls.begin(), VectorOfSmalls.end(), std::mem_fun(&Small::bar));         break;     } }; 

    Also, as you mentioned, it is fairly trivial to replace the &Small::whatever, what a member function pointer and just pass that as a parameter. You can even make it a template too.

    class Big { public:     template<void (Small::*fn)()>     void doit() {         std::for_each(VectorOfSmalls.begin(), VectorOfSmalls.end(), std::mem_fun(fn));     } }; 

    Then you can do:

    Big b; b.doit<&Small::foo>(); b.doit<&Small::bar>(); 

    The nice thing about both this and the regular parameter methods is that Big doesn’t need to be altered if you change small to have more routines! I think this is the preferred method.

    If you want to be able to handle a single parameter, you’ll need to add a bind2nd too, here’s a complete example:

    #include <algorithm> #include <functional> #include <iostream> #include <vector>  class Small { public:     void foo() { std::cout << 'foo' << std::endl; }     void bar(int x) { std::cout << 'bar' << std::endl; } };   class Big { public:     template<void (Small::*fn)()>     void doit() {         std::for_each(VectorOfSmalls.begin(), VectorOfSmalls.end(), std::mem_fun(fn));     }      template<class T, void (Small::*fn)(T)>     void doit(T x) {         std::for_each(VectorOfSmalls.begin(), VectorOfSmalls.end(), std::bind2nd(std::mem_fun(fn), x));     } public:     std::vector<Small *> VectorOfSmalls; };  int main() {     Big b;     b.VectorOfSmalls.push_back(new Small);     b.VectorOfSmalls.push_back(new Small);      b.doit<&Small::foo>();     b.doit<int, &Small::bar>(5); } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

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

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

    • 7 Answers
  • Editorial Team

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

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer If you're using Bash, cut -d $'\001' ... works (see… May 11, 2026 at 3:11 pm
  • added an answer If this is a manual process where you copy across… May 11, 2026 at 3:11 pm
  • added an answer QDataStream supports (de)serialization of some popular Qt objects. You can… May 11, 2026 at 3:11 pm

Related Questions

I have a templated class defined (in part) as template <class T> MyClass {
I'm an experienced C programmer dipping my toes in OO design (specifically C++). I
I've been working on an economy simulator in Java and ran into a roadblock.
I have an existing app with a command-line interface that I'm adding a GUI

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.