Hej! I want to learn using pointers to functions in C. So I write me a little calculator witch contains private operation's functions and one pointer to switch trough them based on entered operation sign. I don't know if it is going to be more efficient then calling individual functions but it just a test.
So this function, inside of Calculator class public section, suppose to set POper
pointer to appropriate operation:
void Calculate() {
switch (m_s) {
default:
printf("Error, wrong operation sign\n");
break;
case '+':
POper = &Calculator::Add;
break;
case '-':
POper = &Calculator::Sub;
break;
case '*':
POper = &Calculator::Multi;
break;
case '/':
POper = &Calculator::Divide;
break;
}
}
When I try to compile fallowing code gcc gives me this type incorrectness warring:
../src/main.cpp: In member function ‘void Calculator::Calculate()’:
../src/main.cpp:16:13: error: cannot convert ‘float (Calculator::*)()’ to ‘float (*)()’ in assignment
POper = &Calculator::Add;
^
../src/main.cpp:19:13: error: cannot convert ‘float (Calculator::*)()’ to ‘float (*)()’ in assignment
POper = &Calculator::Sub;
^
../src/main.cpp:22:13: error: cannot convert ‘float (Calculator::*)()’ to ‘float (*)()’ in assignment
POper = &Calculator::Multi;
^
../src/main.cpp:25:13: error: cannot convert ‘float (Calculator::*)()’ to ‘float (*)()’ in assignment
POper = &Calculator::Divide;
I don't really understand, both POper
and use members are part of Calculator class. What it means with cannot convert ‘float (Calculator::*)()’ to ‘float (*)()’ in assignment
? Whats the difference?