Related Use of Function Pointer

zhaozj2021-02-16  58

Define a pointer to a function, for example:

INT Cuckoo (int Baby);

It can be used to define a pointer to the function:

INT (* PCUCKOO) (INT);

If you want to define the type of all function pointers (can be conveniently declared multiple variables), you can do this:

TYPEDEF INT (*) (int) funpointertype;

Use this type to declare a function pointer as follows:

FunpoIntertype pcuckoo;

FunpoIntertype pshrimp;

Ok, since we have a function pointer, how do you initialize it?

IT's a piece of cake!

PCUCKOO = & coke;

Pshrimp = & cokoo;

OK, how to use them? It is very simple to say nonsense:

PCUCKOO (3);

OR (* PCUCKOO) (3);

It's so simple!

However, in the actual use process, some function parameters are a function pointer, for example:

Void Test (int));

So how do you call TEST? It's simpler:

Test (PCUCKOO);

Oh, is it big me?

Don't worry, sometimes we will encounter such a problem. If you want to pass a member function in a class to Test, can you do this?

Oh, you try:

Class A

{

A ()

{

TEST (TRY);

}

~ A ();

INT TRY (INT NTIMES);

}

Hey, the compiler will tell you unrestricratic, there is a problem!

Why do this look? The answer is also relatively simple: a THIS pointer is hidden in the parameter list of all member functions (what is it?) Blame!

How to do? Still simpler:

The solution is to change this member function to static, so ok!

Ok, what else, I didn't say it clearly? Please send email to alittleshrimp@gmail.com Welcome to harassment! :)

转载请注明原文地址:https://www.9cbs.com/read-18846.html

New Post(0)