Can you make a pointer to a function pointer inside of a class?

81 Views Asked by At

I was trying something silly just to avoid using a single if statement in c++ in Atmel Studio (I'm programming an Atmega328pb for a battery charger but that has relatively little to do with the question except for the GCC version which I guess is supposed to be c++11 but quite a couple of functionalities are missing from what I understand). Is it possible to make an (array of pointers but I guess it's the same thing) pointer to a pointer to a method of a class? and have all of that be a part of the class itself?

Something kinda like this (not the whole thing):

#include <iostream>
typedef void func_ptr_t (void);

class battery{
public:
    bool enable; 
    uint8_t port; 
    func_ptr_t (battery::*func_ptr[2]); 
    void*(battery::*enable_ptr[2])(void); // what I think would be the pointer to a pointer

    battery(uint8_t _battery, bool _enable): enable(_enable), port(_battery) {
        func_ptr[0] = {&battery::set_ports_low};
        func_ptr[1] = {&battery::set_ports_high}; //these two, at least when I tried them, worked successfully to call the methods of a particular object
    }

    void set_ports_low(){}
    void set_ports_high(){}
};

int main() {
    return 0;
}

That's kind of the idea of the program so far, but then I attempted to initialize the pointer to a pointer like this in the constructor enable_ptr[1] = {&battery::func_ptr[0]}; And I get the error (in vs code):

a value of type "void (battery::**)()" cannot be used to initialize an entity of type "void *(battery::*)()"C/C++(144)

Which I guess could be a syntax problem but I don't know if the whole thing is fundamentally wrong with the way I'm establishing it.

Any help or insights would help, I'm more interested in knowing if it could be done rather than its uses here, but still do tell if what I'm trying here is just pointless or if it does have a place in some application.

0

There are 0 best solutions below