Invalid + use + of + non-static + member + Function + Problem

Source: Internet
Author: User
Tags abstract language

# 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.