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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T16:02:31+00:00 2026-06-06T16:02:31+00:00

I have a class Manager and a class Base (with subclasses deriving from Base,

  • 0

I have a class Manager and a class Base (with subclasses deriving from Base, say A : public Base). Inside Manager I create the right subclass of Base but then Base is oblivious to the existence of Manager. I wanted to pass into this created subclass of Base a function object bound to a member method of the Manager class for the A object to invoke. In other words during creation it would be something like this:

void Manager::createThing()
{
FunctionObject func;
if (doExtraWork)
    func.bind(&Manager::someMethod, this);

Base *bla = new A(func);
}

Then inside of A, I would like to be able to detect whether we have extra work to have the Manager carry on us and do so, something like:

A::update()
{
 if (func) //ideally detect if func was bound to someMethod or func is empty
    func(this); //equivalent to the above instance of manager calling someMethod on this instance of A

//do regular stuff after;
}

How can I go about doing this (probably with boost function or something like that)?

  • 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-06T16:02:32+00:00Added an answer on June 6, 2026 at 4:02 pm

    If your FunctionObject type is boost::function<void()>, then you can assign to this from the result of boost::bind, something like this:

    boost::function<void()> f = boost::bind(&Manager::someMethod, this);
    Base *b = new A(f);
    

    where the constructor of A has signature A(boost::function<void()>). Calling a boost::function has slightly more overhead than a virtual function call.

    I don’t know enough about your particular situation to know for sure, but you might end up with a better interface if you actually define an abstract base class representing the functionality of f, and have your constructor for Base and A take an object of the abstract base class instead, like this:-

    struct ThingDoer
    {
        virtual void do() = 0;
    };
    
    class Manager : private ThingDoer
    {
        virtual void do()
        { ... }
    
        void createThing()
        {
            Base *b = new A(this);
        }
    };
    

    where the constructor of A has signature A(ThingDoer*).

    If you’ve previously read about the ‘delegate’ design pattern, you know the kind of thing I mean. In this simple, abstract example, it just looks clunkier and longer than the solution using boost::function, but it does have potential advantages for real software:

    • It lets you give names to the ‘delegate’ type ThingDoer, your implementation of that type, and the function inside it. Also, if you use Doxygen or some other structured comment system, it’s a lot easier to document the delegate class than a function object parameter.
    • It lets you choose in the Manager class and any derived classes whether to inherit from ThingDoer, or have a nested (private or protected) class that inherits ThingDoer.
    • If you want to add an extra function to the interface later (to make it do two different things), it’s easier to do that, and you won’t be tempted to simply add more and more function objects. (But it’s only easier later because you’ve already paid the extra cost of writing your code this way.)
    • If you have programmers from the Java or C# communities, they’ll probably be more familiar with code like this.

    It may be that none of those are advantages in your situation, in which case, definitely use the boost::function / boost::bind solution. I’ve done the same in similar situations. I probably ought to mention too that C++11 has most or all of the function-binding functionality in std:: itself.

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

Sidebar

Related Questions

I have a manager class, which update json data from web, get values... The
I have an sqlConnection manager class like so: public class SQLConn { public string
So here is my dilemma: I have a base class (Let's say a generic
I have a service manager class used to abstract my calls from my MVC
I have an issue with a memory leak. I have a base-class pointer. From
I have something like this: class Base { public: static int Lolz() { return
I have a base class Person and derived classes Manager and Employee . Now,
I have the following manager<->worker situation: class Manager { private: pthread_attr_t workerSettings; pthread_t worker;
I have a simple update method in my manager class and I need to
I have the following: MODEL.PY LIST = (('Manager', 'Manager'),('Non-Manager', 'Non-Manager'),) class Employee(models.Model): fname =

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.