C/C++ programming foundation: function pointer

Before talking about function pointers, we need to understand a concept: how the compiler recognizes and calls functions.
During the compilation of C/C++ programs, the memory has four functional partitions:
1) Code area:
functions are stored.
2) Data area:
static data and global variables are stored.
3) The heap area
stores pointers.
4) The stack area
stores local variables.

Since the function is also stored in the memory, the function must be like other data, occupying the corresponding memory unit in the memory, and each memory unit has an entry address. Therefore, it is not difficult to think that when a program calls a function during operation, it must first find the entry address of this function before executing its corresponding function. We can also directly point to the entry address of a function through a pointer, and call this function through a pointer. This is a function pointer.

Use of function pointers

Ordinary function pointers:
The use of function pointers and variable pointers is not the same. We declare function pointers as follows:

(Function return value type) (pointer) (function parameter list)

Sample program:

#include <iostream>

using namespace std;

void sayName(string name) {
    
    
	cout << "I am " << name << endl;
}

int main() {
    
    
	void (*p)(string name) = &sayName;
	
	p("Bob");
		
	return 0;
} 

The output of the above program operation result:

I am Bob

When making a function pointer point to a function, we can just use the corresponding function name to assign, but it is fine to add the address symbol & before the function name (it is recommended to add).

Class member function pointer:
If you want to make a function pointer point to the public member function of the class, the method of declaration is also different from the ordinary function pointer, because we need to indicate which class the function pointed to by this class member function pointer belongs:

(Function return value type) (Class name:: pointer) (Function parameter list) *

Sample program:

#include <iostream>

using namespace std;

class A {
    
    
	public:
		A(string name):name_(name) {
    
    }
	
	public:
		void sayName() {
    
    
			cout << "I am " << name_ << endl;
		}
		
	private:
		string name_;
};

int main() {
    
    
	A a("Alice");
	A b("Bob");
	
	void (A::*p)() = &A::sayName;
	
	(a.*p)();
	(b.*p)();
		
	return 0;
}

The output of the above program operation result:

I am Alice
I am Bob

When calling through a class member function pointer, it is also different from the use of ordinary function pointers, because we need to explain which object of this class the member function pointer points to, because different instance objects of the same class correspond to the member function The execution result may also be different (it should be said that it may be the same, because in most cases it should be different).
The syntax when calling is as follows:

(Instance object name. class member function pointer) (argument list)

In the above program, we define a pointer p to a member of class A. We first call the sayName of the instance object a of class A through p; then we call the sayName of the instance object b of class A through p.

Moreover, function pointers also support polymorphism.

Sample program:

#include <iostream>

using namespace std;

class A {
    
    
	public:
		A(string name):name_(name) {
    
    }
	
	public:
		virtual void sayName() {
    
    
			cout << "I am " << name_ << endl;
		}
		
	protected:
		string name_;
};

class B:public A {
    
    
	public:
		B(string name, string number):A(name), number_(number) {
    
    }
		
	public:
		void sayName() {
    
    
			cout << "I am " << name_ <<  " and ";
			cout << "My number is " << number_ << endl;
		}
		
	private:
		string number_;
};

int main() {
    
    
	A a("Alice");
	A b("Bob");
	B c("Sandy", "123456");
	
	void (A::*p)() = &A::sayName;
	
	(a.*p)();
	(b.*p)();
	(c.*p)();
		
	return 0;
} 

The output of the above program operation result:

I am Alice
I am Bob
I am Sandy and My number is 123456

In the above program, I define the sayName function of A as a virtual function, so that the newly defined class B inherits from A, and implements a different sayName function in B from its parent class A. At this time, the member function pointer of class A can still point to the sayName function of its subclass B, and the sayName function implemented in B is called.

Guess you like

Origin blog.csdn.net/weixin_45711556/article/details/108907964