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

  • Home
  • SEARCH
  • 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 6175935
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T00:00:39+00:00 2026-05-24T00:00:39+00:00

Hey i’m trying to make a Button template class, which is constructed with the

  • 0

Hey i’m trying to make a Button template class, which is constructed with the the button would recieve when pressed (such as mouse position), and a pointer to the function that should be called.

However buttons often return void and take no arguments (Buttons that you press and something happens: they don’t take any arguments, they’re pressed and then just do something.) so how would i generate the classes member functions since apparently i can’t have void as an argument type?

Here’s the source if it’s helpful:

    template<typename Return = void, typename Arg1 = void, typename Arg2 = void> 
class Button 
{
private:
    boost::function<Return (Arg1, Arg2)> Function;
    //Return (*Function)(Arg1, Arg2);              // this didn't work so i tried boost::function

public:
    void Activate(Arg1, Arg2){ Function(Arg1, Arg2) ;};

    void SetFunction(Return (*Function)(Arg1, Arg2)){
        this->Function= Function;};

    //constructors
    Button(){ Function= 0;};

    Button( Return (*Function)(Arg1, Arg2)){  
        this->Function = &Function; };
};
  • 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-24T00:00:40+00:00Added an answer on May 24, 2026 at 12:00 am

    You can specify a template specification of type void, for example, you could use the following variations of the templated class, button:

    template <typename rtnVal, typename Val1, typename Val2>
    class Button {
    private:
        rtnVal(*Function)( Val1 val1, Val2 val2 );
    
    public:
        Button() : Function( nullptr ) {}
    
        void SetFunction( rtnVal(*func)(Val1, Val2) ) {
            Function = func;
        }
    
        rtnVal RunFunction( Val1 val1, Val2 val2 ) { return Function( val1, val2 ); }
    };
    
    // Special void type, accepting arguments overload:
    template < typename Val1, typename Val2 >
    class Button< void, Val1, Val2 > {
    private:
        void(*Function)(Val1 val1, Val2 val2);
    
    public:
        Button() : Function( nullptr ) {}
    
        void SetFunction( void(*func)(Val1, Val2) ) {
            Function = func;
        }
    
        void RunFunction( Val1 val1, Val2 val2 ) { return Function( val1, val2 ); }
    
    };
    
    // Pure void type:
    template<>
    class Button<void, void, void> {
    private:
        void(*Function)( void );
    
    public:
        Button() : Function( nullptr ) {}
    
        void SetFunction( void(*func)() ) {
            Function = func;
        }
    
        void RunFunction() { 
            return Function();
        }
    };
    

    This then allows you to initialize and use void as arguments, for example, given a void function Print() the following would now be valid:

    void Print()
    {
        std::cout << "Function has been called" << std::endl;
    }
    
    int main()
    {
        Button< void, void, void > btn;
    
        btn.SetFunction( Print );
    
        btn.RunFunction();
    
        std::cout << "Finished";
    }
    

    I hope this helps to clear things up! 🙂

    Note: nullptr is a C++0x keyword, if your compiler hasn’t implemented it use #define nullptr 0

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

Sidebar

Related Questions

Hey there, I'm at the moment trying to make a product management application. The
Hey, I'm trying to make an Address book for my site and I was
Hey there, i am trying to make a little app that will serve multiple
Hey all. Newbie question time. I'm trying to setup JMXQuery to connect to my
Hey having some trouble trying to maintain transparency on a png when i create
Hey guys, I have a little button functionality that is not erring, but also
Hey guys. I'm trying to configure TFS2010 on a home server. I want to
Hey Forum, So i'm trying to find out how to use the source files
Hey all, i am trying to create a report to show how much is
Hey guys, am trying to write to do type casting in java, but i

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.