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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T06:38:17+00:00 2026-06-12T06:38:17+00:00

I’ve been looking at a few signal/slot implementations, and with no exception they were

  • 0

I’ve been looking at a few signal/slot implementations, and with no exception they were pretty complicated, some even relying on MOC and extra code generation, like those of Qt.

I realize there are concerns such as threat safety and whatnot, but for a simple, single threaded scenario, is there something wrong with going for a simple approach, something like:

typedef void (*fPtr)();

class GenericButton
{
public:
    GenericButton() : funcitonToCall(nullptr) {}
    void setTarget(fPtr target) {
        funcitonToCall = target;
    }

    void pressButton() {
        if (funcitonToCall) funcitonToCall();
    }

private:
    fPtr funcitonToCall;
};

void doSomething(){
    std::cout << "doing something..." << std::endl;
}

void doSomethingElse(){
    std::cout << "doing something else..." << std::endl;
}

int main(){
    GenericButton myButton;
    myButton.setTarget(doSomething);
    myButton.pressButton();
    myButton.setTarget(doSomethingElse);
    myButton.pressButton();
}

It is still possible to chain several other methods and pass data in the target void function. So why all the complexity for something as trivial as executing some code when a button gets clicked.

  • 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-12T06:38:18+00:00Added an answer on June 12, 2026 at 6:38 am

    This is a perfectly sensible solution, but don’t restrict yourself to just function pointers. Use std::function which allows you to bind things, call member functions on objects, use lambdas and still resort to a function pointer where it makes sense. Example:

    #include <iostream>
    #include <functional>
    
    using namespace std::placeholders;
    
    
    class GenericButton
    {
    public:
        typedef std::function<void()> fPtr;
        GenericButton() : funcitonToCall(nullptr) {}
        void setTarget(fPtr target) {
            funcitonToCall = target;
        }
    
        void pressButton() {
            if (funcitonToCall) funcitonToCall();
        }
    
    private:
        fPtr funcitonToCall;
    };
    
    struct foo {
        void doSomething() const {
            std::cout << "doing something in a foo..." << std::endl;
        }
    
        static void alternative(int i) {
            std::cout << "And another, i=" << i << "\n";
        }
    };
    
    void doSomethingElse() {
        std::cout << "doing something else..." << std::endl;
    }
    
    int main() {
        GenericButton myButton;
        foo f;
        myButton.setTarget(std::bind(&foo::doSomething, &f));
        myButton.pressButton();
        myButton.setTarget(doSomethingElse);
        myButton.pressButton();
        myButton.setTarget(std::bind(foo::alternative, 666));
        myButton.pressButton();
        myButton.setTarget([](){ std::cout << "Lambda!\n"; });
        myButton.pressButton();
    }
    

    There’s almost always a better solution in C++ than function pointers.

    If you don’t have std::function/std::bind there’s always alternatives in boost that work and you can roll your own std::function alternative without too much work which would be worth doing if you want to make something like this.

    Most of the signal/slot mechanisms that are around date from a time when things like boost::bind was not a viable option. Those days are long gone and you can get something standard and more flexible for little more complexity than just a function pointer.

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

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have been unable to fix a problem with Java Unicode and encoding. The
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm not entirely sure how I managed to jack this up. http://pretty-senshi.com If you
I am using Paperclip to handle profile photo uploads in my app. They upload

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.