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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T14:51:16+00:00 2026-06-13T14:51:16+00:00

How can I have generic function pointer. Consider following class. #ifndef PERSON_HPP_ #define PERSON_HPP_

  • 0

How can I have generic function pointer. Consider following class.

#ifndef PERSON_HPP_
#define PERSON_HPP_

#include <string>

class Person {
public:
  Person() {
  }
  void SetName(std::string person_name) {
    m_name = person_name;
  }
  void setDept(std::string dept_name) {
    m_dept = dept_name;
  }
  void setAge(int person_age ) {
    m_age = person_age;
  }
  std::string getName() {
    return m_name;
  }
  std::string getDept() {
    return m_dept;
  }
  int getAge() {
    return m_age;
  }
private:
  std::string m_name;
  std::string m_dept;
  int m_age;
};

#endif

I want to store the function pointers in a std::vector for setName, setDept , constructor and so on ...

For normal function I could achieve this using following

#include <vector>


int mult(int a) { 
  return 2*a; 
}

int main()
{
    int b;

    std::vector<void *(*)(void *)> v;
    v.push_back((void *(*)(void *))mult);
    b = ((int (*)(int)) v[0])(2);        // The value of b is 2.

    return 0;
}

No Boost allowed in my case.

  • 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-13T14:51:17+00:00Added an answer on June 13, 2026 at 2:51 pm

    If you really need to cast the member pointers to a common type (like void*), you can do:

    template <typename T>
    void* castToVoidStar(T x) {
      return *reinterpret_cast<void**>(&x);
    }
    
    void *afterCast = castToVoidStar(&Person::getName)
    

    However, I do not believe it is wise to do so. C++ has type safety for a reason.

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

Sidebar

Related Questions

I have: void add_all_msgs(std::deque<Message>::iterator &iter); How can I make that function generic, so it
I have made a generic Observer interface and an Observable class, but can't compile
How can I invoke overloaded generic method in IronRuby? I have a .NET class
I am using Preprocessor Directives to define function macros in C. Can I have
I have generic list which must be a preserved order so I can retrieve
Is there a limit on the amount of generic parameters you can have on
I've read posts about why you can't have a (Edit -- generic) (which use
I have a generic Collection and am trying to work out how I can
I want to have a generic event that I can fire that will take
Can anyone help.. I have a generic list like so IList<itemTp> itemTps; itemTp basically

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.