Pointer to functions (general functions and class functions)

Source: Internet
Author: User

The pointer to the function is to use the pointer to save the address of the function as follows:

Void sub (int A, int B); int _ tmain (INT argc, _ tchar * argv []) {void (* t_vp) (int A, int B ); // define the function pointer t_vp = sub; // assign a value to the function pointer (* t_vp) (5, 6); // call the function pointer system ("pause"); Return 0 ;} void sub (int A, int B) {cout <(a + B );}

To assign a value to a function pointer, you need to explain the following step: In t_vp = sub, sub is the function name, which is a function type, not a function pointer. Here is the default conversion. T_vp = & sub, which indicates display conversion. However, the pointer to a class member function cannot be converted by default. The details are as follows:

# Include "stdafx. H "# include <iostream> using namespace STD; Class C {public: int addt (INT, INT) ;}; int _ tmain (INT argc, _ tchar * argv []) {c temp; int (C: * p) (int A, int B); P = & C: addt; // The converted int t_ia = (temp. * p) (5, 6); cout <t_ia <Endl; System ("pause"); Return 0;} int C: addt (int A, int B) {return (a + B );}

In C ++, the class name is added with two colons, for example, C: addt. Only when addt is a static member of class C, C :: addt can 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 display conversion is required.

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.