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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:49:00+00:00 2026-05-27T18:49:00+00:00

#include <iostream> #include <vector> #include <string> #include <ostream> #include <algorithm> #include <boost/function.hpp> using namespace

  • 0
#include <iostream>
#include <vector>
#include <string>
#include <ostream>
#include <algorithm>

#include <boost/function.hpp>
using namespace std;

class some_class
{
public:
  void do_stuff(int i) const
  {
    cout << "some_class i: " << i << endl;
  }
};

class other_class
{
public:
  void operator()(int i) const
  {
    cout << "other_class i: " << i << endl;
  }
};

int main() {
  //             CASE ONE
  boost::function<void (some_class, int) > f;
  // initilize f with a member function of some_class
  f = &some_class::do_stuff;
  // pass an instance of some_class in order to access class member
  f(some_class(), 5); 

  //             CASE TWO
  boost::function<void (int) > f2;
  // initialize f2 with a function object of other_class
  f2 = other_class();
  // Note: directly call the operator member function without
  // providing an instance of other_class
  f2(10);
}


// output
~/Documents/C++/boost $ ./p327
some_class i: 5
other_class i: 10

Question> When we call a function object through boost::function, why we don’t have to provide an instance to the class in order to call this class member function?

Is it because that we have provided such information through the following line?

f2 = other_class();
  • 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-27T18:49:01+00:00Added an answer on May 27, 2026 at 6:49 pm

    You do have to provide an instance to the class, and you are providing one.

    boost::function<void (int) > f2;
    f2 = other_class();
    

    This constructs an other_class object, and assigns that object to f2. boost::function then copies that object, so that by the time you try to call it, you don’t need to instantiate it a second time.

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

Sidebar

Related Questions

#include <boost/algorithm/string.hpp> #include <vector> #include <iostream> #include <string> using namespace std; using namespace boost;
#include <iostream> #include <vector> #include <string> #include <ostream> #include <algorithm> #include <boost/function.hpp> #include <boost/bind.hpp>
#include <iostream> #include <string> #include <vector> #include <algorithm> using namespace std; int main(void) {
Compiling this example #include <boost/bind.hpp> #include <boost/algorithm/string.hpp> #include <algorithm> #include <iostream> #include <vector> using
#include <iostream> using namespace std; // This first class contains a vector and a
#include <boost/ptr_container/ptr_vector.hpp> #include <iostream> using namespace std; using namespace boost; struct A { ~A()
Consider the following : #include <vector> #include <string> #include <iostream> #include <boost/format.hpp> #include <boost/assign.hpp>
#include iostream #include vector class ABC { }; class VecTest { std::vector<ABC> vec; public:
#include <iostream> #include <vector> using namespace std; int main() { vector< vector<int> > dp(50000,
header.h #include <iostream> #include <vector> class CombatLine{ std::stringstream Line; std::vector<std::string> TokenLine; void SetLine(std::string s){

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.