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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T14:20:22+00:00 2026-05-24T14:20:22+00:00

I was looking for a way of adding custom event handlers as a c++

  • 0

I was looking for a way of adding custom event handlers as a c++ class member function. Found that this code actually works.
I need to separate library class and user code, allowing user code to customize library class.
Here is concept of code. Is it correct to do so? Thread safeness is out of scope in my question. Compiler is gcc.
User code is very very neat. no inheritance, virtual functions, templates used

#include <stdio.h>

typedef void fn();

class aaa
{
  public:
  aaa() : customhandler(NULL) {}
  fn *customhandler;
};

// user code start

void dostuff() {
  printf("doing stuff 1\n");
}
void dostuff2() {
  printf("doing stuff 2\n");
}
int main()
{
  aaa aaa1;
  aaa1.customhandler = &dostuff;
  aaa1.customhandler();
  aaa1.customhandler = &dostuff2;
  aaa1.customhandler();
}
  • 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-24T14:20:23+00:00Added an answer on May 24, 2026 at 2:20 pm

    I tried your code and it works fine.
    Just for your information, there is another way to code your function pointer. In which case your code can be simplified just a little. Here I use a slightly different syntax for the typdef for the function pointer, which lets you drop the & symbols.

    #include "stdafx.h"
    #include <stdio.h>
    
    typedef void (*fn)();
    
    class aaa
    {
    public:
        aaa() : customhandler(NULL) {}
        fn customhandler;
    };
    
    void dostuff() {
        printf("doing stuff 1\n");
    }
    void dostuff2() {
        printf("doing stuff 2\n");
    }
    
    int _tmain(int argc, _TCHAR* argv[])
    {
        aaa aaa1;
        aaa1.customhandler = dostuff;
        aaa1.customhandler();
        aaa1.customhandler = dostuff2;
        aaa1.customhandler();
        return 0;
    }
    

    Both ways are just fine, I just thought I’d show you another way to do this.

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

Sidebar

Related Questions

I am looking for strategies, best practices and solutions to adding a custom class
I'm looking for a simple way of adding a refresh mechanism to my UIWebView.
I'm looking a way to enable IP logging with log4net in ASP.NET. I found
My problem is that I was looking for way to use both storyboard and
I'm looking for a way of adding a selection of dynamic cells to the
I am looking at the most efficient atomic way of adding an item to
I'm adding unit tests to a large batch of code and am looking for
This question is related to this one, as I am looking for a way
i'm looking for the simplest way of adding jaxb annotations support to jackson. Jackson
I'm looking for way to create list view as like in IOS with pinned

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.