in Education by
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. 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 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. 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: ((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); How can I solve it and why does it occur? Thanks JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
&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. 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. 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: 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.

Related questions

0 votes
    ________ functions can be built which contain all of the necessary data for evaluating the function. (a) ... of R Programming Select the correct answer from above options...
asked Feb 15, 2022 in Education by JackTerrance
0 votes
    According to ISO C++, dereferencing a null pointer is undefined behaviour. My curiosity is, why? Why ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 24, 2022 in Education by JackTerrance
0 votes
    I have defined an interface in C++, i.e. a class containing only pure virtual functions. I want ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 13, 2022 in Education by JackTerrance
0 votes
    I have defined an interface in C++, i.e. a class containing only pure virtual functions. I want ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 13, 2022 in Education by JackTerrance
0 votes
    I have defined an interface in C++, i.e. a class containing only pure virtual functions. I want ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 13, 2022 in Education by JackTerrance
0 votes
    I would like to know if I can compare 2 member functions with the "...
asked Feb 27, 2022 in Education by JackTerrance
0 votes
    I would like to know if I can compare 2 member functions with the "...
asked Feb 27, 2022 in Education by JackTerrance
0 votes
    I would like to know if I can compare 2 member functions with the "...
asked Feb 26, 2022 in Education by JackTerrance
0 votes
    I would like to know if I can compare 2 member functions with the "...
asked Feb 26, 2022 in Education by JackTerrance
0 votes
    Which of the following package contains functions for reading and displaying satellite data for oceanographic ... Programming Select the correct answer from above options...
asked Feb 12, 2022 in Education by JackTerrance
0 votes
    Which of the following contains functions for processing uniaxial minute-to-minute accelerometer data? (a) ... R Programming Select the correct answer from above options...
asked Feb 10, 2022 in Education by JackTerrance
0 votes
    All compilers I could get my hands on agree that this is fine: template auto foo(Check, T...) - ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 14, 2022 in Education by JackTerrance
0 votes
    // merge.cpp // merges two containers into a third #include #include //for merge() using namespace std; int src1[] = { 2, 3, 4, ... , src1+5, src2, src2+3, dest); for(int j=0; j...
asked Jun 2, 2022 in Education by JackTerrance
0 votes
    I have a function value(x) which is overloaded for many types such that: double value(double x) { return x; } double ... [-Wreturn-local-addr] in the following code: Matrix33 m; m...
asked Apr 24, 2022 in Education by JackTerrance
0 votes
    I'm working on a fairly complex project, a custom encryption routine if you will (just for fun) ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 19, 2022 in Education by JackTerrance
...