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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:03:42+00:00 2026-05-27T05:03:42+00:00

Let’s say I have a Car object which also has an Engine member, and

  • 0

Let’s say I have a Car object which also has an Engine member, and I want to inspect the properties of the object, calling some methods on Car and some methods in Engine. To get te info explicitly I could do

cout << "my car has " << mycar.GetEngine().NCylinders() << " cylinders" << endl;
cout << "my car has " << mycar.NWheels() << " wheels" << endl;

all these calls are of the form mycar.<some method call chain here>. (you can also assume that they all have a compatible return types). How can I have a list of functors, so that I can pass an Car instance and it will execute the calls accordingly.?

I have come up with a solution using <tr1/functional> using nested binds.

#include <iostream>
#include <tr1/functional>
#include <map>

using namespace std;
using namespace std::tr1;
using namespace std::tr1::placeholders;

struct Engine{
    int NCylinders() const {return 12;}
};

struct Car{
    int    NWheels() const {return 4;}
    Engine GetEngine() const {return myEngine;}
private:
    Engine myEngine;
};

int main(){   
    Car mycar;

    map<string,function<double (const Car&)> > carinfos;
    carinfos["cylinders"]   = bind(&Engine::NCylinders,bind(&Car::GetEngine,_1));
    carinfos["wheels"]      = bind(&Car::NWheels,_1);

    map<string,function<double (const Car&)> >::const_iterator info = carinfos.begin();
    for(;info!=carinfos.end();++info){
        cout << "my car has: " << (info->second)(mycar) << " " << info->first << endl;
    }

    return 0;
}

which outputs nicely:

my car has: 12 cylinders
my car has: 4 wheels

But the nested binds can get ugly with longer chains or methods in the middle that have to have fixed arguments and I was wondering if there may be a solution using lambda expressions which could result in something like

 //pseudocode
 carinfos["cylinders"]   = (_1.GetEngine().NCylinder());
 carinfos["wheels"]   = (_1.GetNWheel());

Edit:

@KennyTM and @Kerrek SB have provided excellent answers using the new C++11 lambda expressions. I cannot yet use C++11, so I would appreciate solutions of similar conciseness using C++03

  • 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-27T05:03:43+00:00Added an answer on May 27, 2026 at 5:03 am

    The following, using lambdas rather than binds, doesn’t look too awful:

    typedef std::map<std::string, std::function<int(Car const &)>> visitor;
    
    int main()
    {
      visitor v;
      v["wheels"]    = [](Car const & c) -> int { return c.NWheels(); };
      v["cylinders"] = [](Car const & c) -> int { return c.GetEngine().NCylinders(); };
    
      Car c;
    
      for (auto it = v.cbegin(), end = v.cend(); it != end; ++it)
      {
        std::cout << "My car has " << it->second(c) << " " << it->first << ".\n";
      }
    }
    

    The loop could be wrapped up in a visit(c, v); function.

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

Sidebar

Related Questions

Let's say you have a class called Customer, which contains the following fields: UserName
Let's say I have a drive such as C:\ , and I want to
Let's say I have two files.. I want to compare them side-by-side and see
Let's say I have a main folder in my website named test which contains
Let's say you have some products which have 2 fields, a description and a
Let's say i have an android device that has some extra buttons on it,
Let's say I have a dataset, which can be neatly classified using weka's J48
Let's say I have two assemblies: BusinessLogic and Web. BusinessLogic has an application setting
Let's say I have a Border whose DataContext is an object of type MyViewModel.
Let's say I have the following within my source code, and I want 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.