C + + pointer functions and function pointers

Source: Internet
Author: User
Tags types of functions

The function return type is divided into

    1. Returns the base data type
    2. (pointer function) returns a pointer type that returns a new pointer to a pointer that returns a worthwhile address//pointer function that cannot return a local amount (variable within a function)
    3. Returns the reference type, and the address//reference function that returns the return value directly cannot return a reference to the local amount (variable within the function) without generating a copy. Returning a reference is more straightforward and more readable than returning a pointer

Types of Functions refers to the interface of a function, including the parameter definition and return type of a function

such as the function double Max (double,double);

Double (double,double);

Defining methods

typedef type function type name (formal parameter table)

such as: typedef double Functiontype (double,double);

Use:

such as: Functiontype max,min,average;

(equivalent to declaring three functions at the same time double Max (double,double);d ouble min (double,double); double average (double,double);)

function Pointers is a pointer variable that points to a function

Defining methods

    1. Type (* pointer variable name) (formal parameter list)
    2. function type * pointer variable name

such as: Double (*FP) (double,double);

Functiontype *FP;

When a function pointer is used as a function parameter, the address of the function can be passed, and different functions are called through the arguments.

Example:

1#include <iostream>2 using namespacestd;3 //Defining the Functiontype function type4typedefDoubleFunctiontype (Double,Double);5 //declaration of three functions min max average6 Functiontype min,max,average;7 //defining function Pointers FP8Functiontype *FP;9 //function callfunction with function pointersTen DoubleCallFunction (Functiontype *,Double,Double); One intMain () A { -     DoubleA=4.3, b=3.6; -cout<<"min ="<<min (b) <<"\tmax ="<<max (b) <<"\tave ="<<average (b) <<Endl; the     //function pointer get function address -Fp=min; cout<<"*fb min ="<<callfunction (fp,a,b); -Fp=max; cout<<"\T*FB max ="<<callfunction (fp,a,b); -Fp=average; cout<<"\T*FB Ave ="<<callfunction (fp,a,b) <<Endl; +cout<<"Call min ="<<callfunction (min,a,b); -cout<<"\tcall max ="<<callfunction (max,a,b); +cout<<"\tcall Ave ="<<callfunction (average,a,b) <<Endl; A  atCin.Get(); -     return 0; - } - DoubleMinDoubleADoubleb) - { -     returnA>b?b:a; in } - DoubleMaxDoubleADoubleb) to { +     returnA>b?a:b; - } the DoubleAverageDoubleADoubleb) * { $     return(A+B)/2;Panax Notoginseng } - DoubleCallFunction (Functiontype *FP,DoubleADoubleb) the { +     returnFP (A, b); A}

C + + pointer functions and function pointers

Related Article

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.