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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T06:44:52+00:00 2026-05-24T06:44:52+00:00

I need to pass something like a pointer that takes anything as a function

  • 0

I need to pass something like a pointer that takes anything as a function parameter. You know, something without any predefined type or a type that can take anything like this:

 void MyFunc( *pointer ); 

And then use it like:

char * x = "YAY!";
MyFunc(x);

int y = 10;
MyFunc(&y);

MyObj *b = new MyObj();
MyFunc(b);

And I don’t want to use templates because I am mostly using C in my project.
Is there anything that can be used here except a function macro?

  • 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-24T06:44:54+00:00Added an answer on May 24, 2026 at 6:44 am

    In C++, Boost.Any will let you do this in a type-safe way:

    void func(boost::any const &x)
    {
        // any_cast a reference and it
        // will throw if x is not an int.
        int i = any_cast<int>(x);
    
        // any_cast a pointer and it will
        // return a null pointer if x is not an int.
        int const *p = any_cast<int>(&x);
    }
    
    // pass in whatever you want.
    func(123);
    func("123");
    

    In C, you would use a void pointer:

    void func(void const *x)
    {
        // it's up to you to ensure x points to an int.  if
        // it's not, it might crash or it might silently appear
        // to work. nothing is checked for you!
        int i = *(int const*)x;
    }
    
    // pass in whatever you want.
    
    int i = 123;
    func(&i);
    
    func("123");
    

    You seem adverse to it but I’ll recommend it anyway: if you’re using C++, embrace it. Don’t be afraid of templates. Things like Boost.Any and void pointers have a place in C++, but it is very small.

    Update:

    Well , I am making a small signals – slots – connections library to be
    used with my gui toolkit. So that I can get rid of the Ugly WNDPROC. I
    need these pointers for the connections.

    If you need multi-target signals, Boost.Signals already provides a full and tested signals/slots implementation. You can use Boost.Bind (or std::bind, if you’ve got a C++0x compiler) to connect member functions:

    struct button
    {
        boost::signal<void(button&)> on_click;
    }
    
    struct my_window
    {
        button b;
    
        my_window()
        {
            b.on_click.connect(std::bind(&my_window::handle_click,
                                         this, std::placeholders::_1));
        }
    
        void handle_click(button &b)
        {
        }
    
        void simulate_click()
        {
            b.on_click(b);
        }
    };
    

    If you only want a simple callback, Boost.Function (or std::function if you’ve got a C++0x compiler) will work well:

    struct button
    {
        std::function<void(button&)> on_click;
    }
    
    struct my_window
    {
        button b;
    
        my_window()
        {
            b.on_click = std::bind(&my_window::handle_click,
                                   this, std::placeholders::_1);
        }
    
        void handle_click(button &b)
        {
        }
    
        void simulate_click()
        {
            b.on_click(b);
        }
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to pass multiple arguments to a function that I would like to
Is it possible to pass functions by reference? Something like this: function call($func){ $func();
I have a C function that takes several arguments of double indirected pointers. something
I need to pass a pointer through a scripting language which just has a
I need to pass the $route to its inner function,but failed: function compilePath( $route
I have a javascript function (class) that takes a function reference as one paremter.
I need to pass an ID and a password to a batch file at
I need to pass a regex substitution as a variable: sub proc { my
I need to pass an array from JavaScript to a page method in C#.
I need to pass audio data into a 3rd party system as a 16bit

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.