简体   繁体   English

如何将函数分配给函数指针?

[英]How to assign functions to a function pointer?

I am coding a Gameboy Emulator, and for the CPU's instructions I use this struct here (in cpp.hpp) to store info about them.我正在编写一个 Gameboy 模拟器,对于 CPU 的指令,我在这里(在 cpp.hpp 中)使用这个结构来存储有关它们的信息。 The map is used to access all this information through a key equal to its personal opcode:该映射用于通过等于其个人操作码的密钥访问所有这些信息:

    struct instruction {
        std::string name;    //name of the instruction
        int cycles;          //clock cycles required to be executed
        int paramNum;        //number of params accepted
        void* function;      //code to be executed
    };
    class Cpu {
        private:
            std::map<unsigned char, instruction> instrMap;
            void Cpu::fillInstructions(void);
            instruction addInstruction(std::string, int, int, void*);
        public:
            void add_A_n(unsigned char);
    }

Then in the cpu.cpp I have for example one of the function I want to cast to a function pointer in order to be assigned to the field of the struct instruction.然后在 cpu.cpp 中,例如我想转换为函数指针的函数之一,以便分配给结构指令的字段。 So I have this code:所以我有这个代码:

    void Cpu::add_A_n(unsigned char n) {
        //body     
    }
    void Cpu::addInstructions(std::string name, int cycles, int paramNum, void* function) {
        instruction i = {name, cycles, paramNum, function};
        return i;
    }
    void Cpu::fillInstructions() {
        instrMap[0x80] = Cpu::addInstruction("ADD A, n", 4, 0, (void*)&Cpu::add_A_n);
    }

The goal is to fetch the opcode from the memory, then to use this opcode to retrieve from the map the information about the relative instruction, and finally to execute its function by using a switch case to select the right one:目标是从内存中获取操作码,然后使用该操作码从映射中检索有关相关指令的信息,最后通过使用 switch case 选择正确的来执行其功能:

    ((void (*)(void))instrMap[0x80].function)(); //for 0 params
    ((void (*)(unsigned char))instrMap[0x90].function)((unsigned char)operand); //for 1 param

My goal is to cast the all the functions, even the ones who requires some parameters, to the one in the struct.我的目标是将所有函数,甚至是那些需要一些参数的函数,转换为结构体中的函数。

The respective function it is correctly executed, but a warning is raised:它正确执行了相应的功能,但会引发警告:

warning: converting from 'void (Cpu:: )()' to 'void ' [-Wpmf-conversions] instrMap[0x80] = Cpu::addInstruction("ADD A, n", 4, 0, (void*)&Cpu::add_A_n);警告:从“void (Cpu:: )()”转换为“void ”[-Wpmf-conversions] instrMap[0x80] = Cpu::addInstruction("ADD A, n", 4, 0, (void*)&Cpu ::add_A_n);

How can I solve it and why does it occur?我该如何解决它以及它为什么会发生? Thanks谢谢

&Cpu::add_A_n returns a pointer to a member function , which is very different from an ordinary function pointer, and the two can't be mixed. &Cpu::add_A_n返回一个指向成员函数指针,这与普通的函数指针有很大的不同,两者不能混用。 The weirdness around pointer to member functions is to account for the fact that non-static member functions all require a this instance in order to call the function.指向成员函数的指针的奇怪之处在于,非静态成员函数都需要一个this实例才能调用该函数。

In your case, if a function like add_A_n really doesn't depend on this , just make it static , or a non-member function:在您的情况下,如果像add_A_n这样的函数真的不依赖于this ,只需将其add_A_n static或非成员函数:

class Cpu {
    ...
    static add_A_n(unsigned char);
};

This way, it no longer needs a this to be called, and &Cpu::add_A_n becomes an ordinary function pointer.这样,就不再需要调用this了, &Cpu::add_A_n就变成了普通的函数指针。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM