C/C ++ left-valued essence (3) Left-valued conversion-from function to pointer Conversion

Source: Internet
Author: User

Essence of C/C ++ left

(3) Conversion of left values

 

3. Conversion from function to pointer

 

The purpose of converting a function into a pointer is the same as that of converting an array to a pointer. It is used to convert a symbol into a numeric value to facilitate expression computation. This provision stipulates:

 

AFunction DesignatorIs an expression that has function type. doesn t when it is the operand ofSizeofOperator or the unary&
Operator, a function designator with type ''function returningType''Is converted to an expression that has type ''' pointer to function returningType''.

 

Except for a few, a function indicator with the function type is converted to a pointer to the function entity. In C, the conversion from a function to a pointer does not belong to the conversion from the left value, because the function in C is neither the left value nor the right value, the clause in C does not indicate the left value of the conversion. However, for C ++, the function belongs to the left value, so the conversion belongs to the left value conversion, and the result is a right value pointer.

There is an exception in the left valuability of C ++ functions, that is, non-static member functions are not left values. At first, I was very confused, because in the abstract nature, non-static member functions do not conform to the meaning of the Left value of C ++. I once sent an email to Dr. Bjarne stroustrup, the founder of C ++, asking him this question. In his reply, BS said, he believes that this rule is not very elegant in terms of technical processing to distinguish between common and non-static member functions. That is to say, this is a man-made rule. Because non-static member functions are excluded from the left value category, non-static member functions do not convert from functions to pointers. Non-static member function pointers must be obtained through the & operator. For example:

 

Struct

{

Void Foo (void );

};

 

Void (A: * p) (void) = A: Foo; //

Void (A: * q) (void) = & A: Foo; // B

 

A is incorrect because the implicit conversion of non-static member functions does not exist. B is correct.

For function calling using function pointers, there are two methods:

 

P (); //

(* P) (); // B

 

Both methods are valid. The function call expression requires that the type of Its suffix expression operand be a function pointer. When using a common function name for function calling, the function name is first converted to a function pointer before calling. This is the reason for the first method. In the second method, * is the unreferencing operator. The result of unreferencing a function pointer is the function type pointed to by the pointer, then, the function type is converted back to the function pointer type by converting the function to the pointer, and then the function is called. The two methods are similar. The second method can also generate some interesting forms, such:

 

(*** P )();

(*********** P )();

(************************* P )();

 

These quote operators can be infinitely filled in the above similar way, but the results are the same. You only need to continuously perform the unreference and function pointer conversion.

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.