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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T23:50:47+00:00 2026-06-11T23:50:47+00:00

I have made vectors that contain functions, but they had no argument list. Also,

  • 0

I have made vectors that contain functions, but they had no argument list. Also, they were not inside of a class. I have a class named Dialog, and I need to store function-pointers with a specific signature. Here is my typedef for these functions:

typedef INT_PTR (*MsgHandler)(WPARAM,LPARAM);

But, since the vector that contains these MsgHandler‘s will be in my Dialog class, which will be inherited by my CMainWnd class, when I try to push_back the function, the signature of the function is different than that of MsgHandler. Here is my code, the 4 variations of trying to push_back the function in the vector, and the resulting errors for each of them:

typedef INT_PTR (*MsgHandler)(WPARAM,LPARAM);

class Dialog
{
protected:
    Dialog(void); // Must be inherited
    vector<MsgHandler> Handlers;
}

class CMainWnd : public Dialog
{
public:
    INT_PTR MyHandler(WPARAM wp, LPARAM lp) {
        return TRUE;
    }

    CMainWnd(void) {
        // Attempt 1: Handlers.push_back(MyHandler);
        // Attempt 2: Handlers.push_back(&MyHandler);
        // Attempt 3: Handlers.push_back(CMainWnd::MyHandler);
        // Attempt 4: Handlers.push_back(&CMainWnd::MyHandler);
    }
};

Attempt 1 yields the following error:

error C3867: 'CMainWnd::MyHandler': function call missing argument list; use '&CMainWnd::MyHandler' to create a pointer to member

Attempt 2 yields:

error C2276: '&' : illegal operation on bound member function expression

Attempt 3 yields:

error C3867: 'CMainWnd::MyHandler': function call missing argument list; use '&CMainWnd::MyHandler' to create a pointer to member

Attempt 4 yields:

error C2664: 'std::vector<_Ty>::push_back' : cannot convert parameter 1 from 'INT_PTR (__thiscall CMainWnd::* )(WPARAM,LPARAM)' to 'const MsgHandler &'

I think attempt 4 is the closest to being correct, but as stated previously since the function is a member, it alters the signature.
How can I store function-pointers in the vector that is defined in the inherited class Dialog that:

  • Have a return type of INT_PTR
  • Have two parameters, the first being a WPARAM, and the second being a LPARAM
  • Are a member of the derived class whose constuctor is the one adding them to the vector

I have heard of using boost::function for doing things like this, but I have looked up documentation for it and I have no idea how to use it, it all seems confusing to me. I would very much like to just add the functions to the vector as if they were variables instead of bogging down my code with bind operations and what not. (May just be because I am ignorant when it comes to boost functions).

What am I doing wrong here, or can I use boost::function to do this? For the boost::function way, I have tried declaring the vector as vector<boost::function<INT_PTR(WPARAM,LPARAM)>> and tried adding MyHandler to it, but that did not work. I would prefer not to use boost if I don’t have to, but if someone suggests the boost way of doing this, please clarify how I could do this?

  • 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-11T23:50:48+00:00Added an answer on June 11, 2026 at 11:50 pm

    This is where lambdas really shine if you have access to C++11. I’m going to change your code a little bit just because it’s slightly easier to get it to work for me, but you should be able to fix it up for your needs quite easily:

    #include <vector>
    #include <functional>
    #include <iostream>
    
    class Dialog
    {
    protected:
        Dialog() { } // Must be inherited
        std::vector<std::function<bool (int, int)>> Handlers;
    };
    
    class CMainWnd : public Dialog
    {
    public:
        bool MyHandler(int wp, int lp) 
        {
            std::cout << "(" << wp << ", " << lp << ")\n";
            return true;
        }
    
        CMainWnd() 
        {
           Handlers.push_back([this](int wp, int lp) -> bool { return this->MyHandler(wp, lp); });
           std::cout << Handlers[0](1,1) << "\n";
        }
    };
    
    int main()
    {
        CMainWnd c;
        return 0;
    }
    

    Edit: When using C++03 and boost instead:

    #include <boost/function.hpp>
    #include <boost/bind.hpp>
    
    class Dialog
    {
    protected:
        Dialog() { } // Must be inherited
        std::vector<boost::function<bool (int, int)> > Handlers;
    };
    
    class CMainWnd : public Dialog
    {
    public:
        bool MyHandler(int wp, int lp) 
        {
            std::cout << "(" << wp << ", " << lp << ")\n";
            return true;
        }
    
        CMainWnd() 
        {
           Handlers.push_back(boost::bind(&CMainWnd::MyHandler, this, _1, _2));
           std::cout << Handlers[0](1,1) << "\n";
        }
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have made a jQuery toggle for a menu that I had in mind.
I have made some helper functions that run a simulation using a lot of
I have made a code that read data from flash Nand (without filesystem). fd
I have made a cart and the cart has remove item button, but the
I have made a map of functions. all these functions are void and receive
I have made some research on Stackoverflow about reverse for loops in C++ that
we have a C++ class which basically reads and writes vectors from a binary
I have made a winform app where i have used vectors of array[n][n] type
I have a class Matrix which contains ArrayList<ArrayList<Double>> matrix; inside, and I want to
I have two 3D vectors called A and B that both only have a

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.