#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:
- [Qt] error: call to non-static member function without an object argument
- Failed to import static file of vue3 static resource. There is no static folder
- [C + +] C + + overload operator = must be a nonstatic member function?
- PHP Fatal error: Call to a member function query() on a non-object in
- [How to Solve]Warning: connect.static is not a function
- non-static variable this cannot be referenced from a static context
- non-member function ‘xxxx’ cannot have cv-qualifier
- Fatal error: Call to a member function bind_param() on a non-object in
- call to member function bind_param() on boolean………..
- 【ERROR_2】Call to a member function bind_param() on a non-object
- Call to a member function fetch_assoc() on a non-object
- Nuxt cannot find the static resource in static
- How to use C + + function pointer array
- About the prompt: invalid use of incomplete type
- Module ‘XX’ has no ‘XX’ member pylint (no member)
- Use of rep function in R
- The use of C + + template function and lambda expression
- How to Use the Reverse() Function
- error: ‘CLOCK_MONOTONIC‘ undeclared (first use in this function)
- nvalid default value for prop “params“: Props with type Object/Array must use a factory function to