"C++ Primer" reading notes-Chapter 07 Function Pointer

Author: Ma Zhifeng
link: https: //zhuanlan.zhihu.com/p/23926784
Source: know almost
copyrighted by the author. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source.

statement:

  • The content in the article is collected and compiled from "C++ Primer Chinese Edition (5th Edition)", and the copyright belongs to the original book.
  • The original book has more detailed and wonderful interpretations, please buy genuine books for learning.
  • This article is only for learning and communication, any form of reprinting is prohibited

content

  • Declare function pointer
  • Function pointer as formal parameter
  • Function pointer as return value

Declare function pointer

The pointer points to a specific type, so what is the type of the function?

The type of the function is determined by its return type and the type of the formal parameters , and has nothing to do with the function name

bool lengthCompare( const string&, const string& );

The type of the function is

bool ( const string&, const string& )

Then you can declare the function pointer in the following form

bool (*pf)( const string&, const string& );

You can replace the function name with a pointer

Use function pointers

The function pointer has the following special features:

  1. The function name is automatically converted into a pointer, and the address character is not necessary
  2. You can directly use the pointer to the function to call the function, dereferencing is not necessary
pf = lengthCompare;  
pf = &lengthCompare;

transfer

bool b1 = pf( "Hello", "goodbye" );  
bool b2 = (*pf)( "Hello", "goodbye" );  
bool b3 = lengthCompare( "Hello", "goodbye" );

Function pointer parameter

void useBegger( const string &s1, const string &s2,   
    bool pf( const string &, const string & ));  
void useBegger( const string &s1, const string &s2,   
    bool (*pf)( const string&, const string& ));

The above two functions are legal. When
we use function pointers as formal parameters, we
can explicitly define the formal parameters as pointers to the function.
You can also directly use the function type, which will automatically be converted to a function pointer.

Use type alias or decltype to simplify

//函数类型的别名  
typedef bool Func(const string&, const string&);    
typedef decltype(lengthCompare) Func2;  

//函数指针的别名
typedef bool(*FuncP)(const string&, const string &);  
typedef decltype(lenghtCompare) *FuncP2;

Return function pointer

int (*f1(int))(int *, int);

This looks complicated and difficult to understand, and we have many ways to simplify

  1. Type alias
  2. decltype
  3. Tail return type

We have finished reading the contents of the first six chapters here!

I will find some exercises to do later, there are two aspects currently in mind

  1. leetcode
  2. Use opencv library to do some simple image processing

Guess you like

Origin blog.csdn.net/qq_26751117/article/details/53442412