0

I'm pretty new to C++ I have this code:

in my class.hpp

class Dummy {
    private:
        void    f1(void);
        void    f2(void);
        void    f3(void);
        void    f4(void);
    public:
        void caller(std::string id);
};

in my class.cpp

void Dummy::caller( std::string id ) {
    // something something about qualifiers requires Dummy::*f instead of just *f
    void    (Dummy::*f[4])() = {&Dummy::f1, &Dummy::f2, &Dummy::f3, &Dummy::f4};
    string v[4] = {"f1", "f2", "f3", "f4"};
    
    for (int i = 0; i < 4; i++) {
        if (id == v[i]) {
            (*f[i])();
            break ;
        }
    }
}

this (*f[i])() is valid in C code, but for some reason in C++, it shows me this error which i googled but was unlucky and found nothing that useful, except for std::invoke which is in C++17 (?), and I'm bound to C++98 The error:

Class.cpp:41:5: error: indirection requires pointer operand ('void (Dummy::*)()' invalid)
                        (*f[i])();
                         ^~~~~
sickl_
  • 21
  • 5
  • You have pointers to member functions, which require a valid object. Did you intend to call it on the `this` object? – UnholySheep Sep 23 '21 at 14:46
  • the task I was assigned to was to make a public method in the class that calls the private methods that it will store in a pointer-to-function array, and call the right one based on a parameter that is passed to it, skeleton code shouldn't be modified (only use a for loop and an if statement), inside the if statement call the appropriate function from the p2f array and break – sickl_ Sep 23 '21 at 14:54
  • it does! still confused but thank you (I almost never see gibberish like this in C, this->*f[i]) – sickl_ Sep 23 '21 at 15:00

1 Answers1

0

well, this is not how c++ works... just because you define functions like

    void    f1(void);
    void    f2(void);
    void    f3(void);
    void    f4(void);

doesnt mean you can access them or handle then like you were working with an array

but that is the key to solve that, you can create an array of functions and call it by index


here is a short example of how you can achieve that:

#include <iostream>

void f1(void)
{
  std::cout << "you are in f1" << std::endl;
}

void f2(void)
{
    std::cout << "you are in f2" << std::endl;
}
void f3(void)
{
    std::cout << "you are in f3" << std::endl;
}

void f4(void)
{
    std::cout << "you are in f4" << std::endl;
}


void (*p[4]) (void);

int main(void)
{
  int result;
  int i, j, op;

  p[0] = f1;
  p[1] = f2;
  p[2] = f3;
  p[3] = f4;

    for(auto i= 0; i<4; ++i)    
    {
        (*p[i])();
    }
    
    return 0;
}
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
  • They were put in an array: `void (Dummy::*f[4])() = {&Dummy::f1, &Dummy::f2, &Dummy::f3, &Dummy::f4};`. The problem is how it's being called, as a comment mentions – Kevin Sep 23 '21 at 14:47
  • i KNOW that this works, but how do you do it from inside a public method of the class with the private methods of the class? – sickl_ Sep 23 '21 at 14:56
  • Search for pointers to member functions in c++. As an example see here - https://stackoverflow.com/questions/9568150/what-is-a-c-delegate/9568226#9568226 – moi Sep 23 '21 at 15:02