error: invalid use of non-static member function

Transfer learning: https://blog.csdn.net/bill_ming/article/details/6872165
#include < iostream>

using namespace std;
class A
{
public:
A (int I)
{
A = I;

} int fun (int) b
{
return a * c + b;

} int c;
private :
int a;
};

int main()
{
A x(18);
int A: : * PC;
PC = & amp; A::c;
x. * PC = 5;
int (A: : * pfun) (int);
pfun = A: : fun;
A *p = & x;
cout & lt; < (p-> *pfun)(10)< < endl;

return 0;
}

error: error: invalid use of non-static member function ‘int A::fun(int)’|

int (A::*pfun)(int)=A::fun;
=
int (A::*pfun)(int)=& A::fun;
 
In C++, for something that consists of the class name plus two colons plus A member name (quilified-id), such as: A::x, A::x can only represent an left value if x is A static member of class A.
The default conversion from a function type to a function pointer type only works if the function type is an lvalue. For non-static member functions, there is no default conversion from function type to function pointer type, so the compiler does not know what to do with the
p = A::f
.
 
(1) To say that the address of the function is not certain, because the non-static member function pointer can have polymorphic behavior, so, at least at compile time, its address is really not determined.
(2) although all compilers will certainly generate only one copy of the code for a class’s non-static member functions (either at compile time or at run time), this is different from non-static member variables, which have a separate copy for each object. But In my opinion, this is still an implementation detail of the compiler, not a language feature. From an abstract language point of view, non-static member functions, like non-static member variables, cannot be left valued without an object instance.

Read More: