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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T05:23:42+00:00 2026-05-31T05:23:42+00:00

This question is similar to what I’m trying to do Calling C++ member function

  • 0

This question is similar to what I’m trying to do Calling C++ member function pointer from a struct .
However my structure contains a member function pointer that is defined in a different class then the one the structure is defined and used in. Here is some example code of how my classes, structures and function pointers are laid out.

// Alpha.h:
class Alpha{
public:
    void function1(char name[], int number);
    void function2(char name[], int number);
    void function3(char name[], int number);

    typedef void (Alpha::*My_func_ptr)(char name[], int number);

    static My_func_ptr functionTable[];
};

// Alpha.cpp:
#include "Alpha.h"

Alpha::My_func_ptr Alpha::functionTable[] = {
    &Alpha::function1, 
    &Alpha::function2, 
    &Alpha::function3
};

void Alpha::function1(char name[], int number)
{
    //some stuff
}

void Alpha::function2(char name[], int number)
{
    //some stuff
}

void Alpha::function3(char name[], int number)
{
    //some stuff
}

// Beta.h:
#include "Alpha.h"

typdef struct{
    char bName[10];
    Alpha::My_func_ptr fptr;
}ptr_structure;

class Beta{
public:
      void betafunction();

      Alpha alphaobject;
      ptr_structure str_array[3];
};

// Beta.cpp:
#include "Beta.h"

void betafunction()
{
    str_array[0].fptr = alphaobject.functionTable[0];
    str_array[1].fptr = alphaobject.functionTable[1];
    str_array[2].fptr = alphaobject.functionTable[2];

    (str_array[0].fptr)("name", 1); //gives error expression must have 
                                    //(pointer-to-)  function type

    (this->*str_array[0].fptr)("name", 1);
    //error pointer-to-member selection class types are incompatible "Beta" and "Alpha"

    //sample function pointer call using function table from other class,
    //this syntax compiles and runs without error.
    (alphaobject.*Alpha::functionTable[0]("name", 1); 
}

As you can see I can call the function pointer from an array, but can’t seem to figure out how to call a function pointer from inside an array of structures.

  • 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-31T05:23:43+00:00Added an answer on May 31, 2026 at 5:23 am

    When calling a through member function pointer, you need to have an instance of the object associated with that pointer:

     (alphaobject.*(str_array[0].fptr))("name", 1)
      ^^^^^^^^^^^
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This question is similar to GWT Table that supports sorting, scrolling and filtering However
This question is similar to this other one , with the difference that the
This question is similar to this except that the substring to be replaced is
Follow up to this question This (similar version from old link) works in SQL
This question is similar to another one I asked , but whereas in that
I'll preface this question with the note that I have looked at this similar
This question is similar to How can I specifiy what access I need from
I realize that there was a similar question asked here , but this is
This question is similar to this question , bit the big difference is that
This question is similar to Find conditions like 'NOT EXISTS' except that it's working

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.