# Include <iostream>
Using namespace STD;
Class
{
Public:
A (int I)
{
A = I;
}
Int fun (int B)
{
Return a * C + B;
}
Int C;
PRIVATE:
Int;
};
Int main ()
{
A x (18 );
Int A: * PC;
PC = & A: C;
X. * Pc = 5;
INT (A: * pfun) (INT );
Pfun = A: fun;
A * P = & X;
Cout <(p-> * pfun) (10) <Endl;
Return 0;
}
Error: Invalid use of non-static member function 'int A: Fun (INT) '|
INT (A: * pfun) (INT) = A: fun;
Change
INT (A: * pfun) (INT) = & A: fun;
In C ++, a class name is composed of two colons and a member name (the learning name is "quilified-ID"), such as A: X, only when X is a static member of Class A can a: x represent a left value.
The default conversion from the function type to the function pointer type is only available when the function type is left. All non-static member functions do not have this default conversion from the function type to the function pointer type, so the compiler does not know this
P = A: F
What should I do.
(1) The function address cannot be determined. Because non-static member function pointers can have polymorphism, the address cannot be determined at least during compilation.
(2) Although all compilers are willing to generate only one copy of the code of the non-static member functions of the class (whether at compile time or runtime), this is different from that of non-static member variables, each object of a variable has an independent copy. -- But I think this is still the implementation details of the compiler, rather than the language features. From the perspective of abstract language features, non-static member functions are the same as non-static member variables. When there is no object instance, they cannot form the left value.
Http://topic.csdn.net/t/20060906/11/5002223.html