0

Currently I have created a class called A that creates an array of pointers to functions as well as declared the function's prototypes, please view the A.h below.

#ifndef A_H
#define A_H

class A
{
    public:

        //Function Definitions that will be called from the seq function below.

        A::A()
        //prototype functions that will be pointed to from array 
        void f1(int);
        void f2(int);
        void f3(int);

        void (*funcs[3])(int);
        //Destructor method
        virtual ~A();    
};

Then I have A.cpp defined below with the function definitions of f1, f2, f3.

A::A()
{

}

void A::f1(int a){ cout << "This is f1: " << endl; }
void A::f2(int b){ cout << "This is f2: " << endl; }
void A::f3(int c){ cout << "This is f3: " << endl; }

//Would this be valid?
funcs[0] = f1;
funcs[1] = f2;
funcs[2] = f3;

A::~A()
{
    //dtor
}

How would I access the array from main.cpp

include "A.h"
.
.
.


int main()
{
    //This doesn't work, what is the proper way to access the pointers to functions?
    A a;
    a.funcs[0](1);

}

Should print out "This is f1: ". This has really been bugging me, i've been reading different threads and even used typedefs but to no avail. Any help would truly be appreciated. If you need anymore details please let know.

Kevag6
  • 135
  • 1
  • 1
  • 10
  • _`//Would this be valid?`_ No. You can't use non `static` class member functions there. I'd recommend you use `std::function` and a lambda expression to capture `a`. –  Feb 04 '18 at 15:53
  • Your functions are not functions. They are class methods, which are not functions. That's why your compiler is refusing to compile this. Declare your class methods as `static` if you want them to be real functions, instead of class methods. – Sam Varshavchik Feb 04 '18 at 15:54
  • 1
    Possible duplicate of [C++: Array of member function pointers to different functions](https://stackoverflow.com/questions/3565825/c-array-of-member-function-pointers-to-different-functions) – Algirdas Preidžius Feb 04 '18 at 15:54
  • @Sam _"They are class methods, which are not functions."_ This terminology is new to me. I always thought _class member function_ is the correct term, no? And why these aren't functions? –  Feb 04 '18 at 15:55
  • @AlgirdasPreidžius Thanks for the link I guess before I ask a question a should dig deeper into my research. i'll let you know if I figure it out. Thanks again. – Kevag6 Feb 04 '18 at 16:05
  • @Kevag6 Here's another link: https://stackoverflow.com/questions/400257/how-can-i-pass-a-class-member-function-as-a-callback –  Feb 04 '18 at 16:11
  • @TheDude - see https://stackoverflow.com/q/18282330/2785528 for terminolgy discussion and answers. It surprised me. – 2785528 Feb 04 '18 at 16:12
  • @DOUGLASO.MOEN Though I find that terminology just confusing. –  Feb 04 '18 at 16:13
  • 1
    @Kevag6 "_I guess before I ask a question a should dig deeper into my research._" Yes, it is expected of you, to do research, before asking. And I don't know about digging deeper, since it was the second link, in the search engine, that I am using.. – Algirdas Preidžius Feb 04 '18 at 16:26
  • They are not functions because you just can't invoke them, by themselves, like "`f1();`". You must have a class instance whose method can be invoked, like "`a.f1();`". – Sam Varshavchik Feb 04 '18 at 17:29

0 Answers0