C++ study notes six-pointers to functions and constructors

One of the class members to be explained first-the constructor, the role of the constructor is to initialize the class members. , The special member function of the constructor is different from other member functions. The constructor has the same name as the class and has no return type. The same as other member functions, the constructor also has a formal parameter list (may be empty) and a function body. A class can have multiple constructors, and each constructor must have a different number or type of formal parameters due to other constructors. Here is an example to illustrate:

//首先定义一个类
struct hanrui
{
    
    
public//可以不写,因为struct默认为public
int test;//定义一个test
string name;

}

We see that our class named hanrui contains two data, test and name, so if you want to initialize it, you can

struct hanrui
{
    
    
public//可以不写,因为struct默认为public
int test;//定义一个test
string name;
hanrui()
{
    
    
test=1;
name=“hanrui”;
}
}

Finally output the observation results.
Insert picture description here
Therefore, the constructor can be simply understood as initializing class members.
The second point of knowledge is the pointer to the function. The pointer can be understood as the address where the data is stored. If a data is compared to a guest, then the pointer is the house number of the room. The guest can be accessed through this house number, that is, the pointer = address . For general data types, that is, for ordinary people's tenants, we use general services, that is, we only need to explain the type of pointer, the name of the pointer, and some basic definitions.
The following is the definition of the pointer

vector<int> *pvec; //pvec 能够指向vector<int>
int *p1 //p1能够指向整数
double *dp //dp能够指向double类型数据

The previous article mentioned the concept of iterators, so pointers are actually iterators of arrays; the
above is for general customers, then functions can be understood as VIP customers. For VIPs, in addition to the rights of general customers In addition, you must enjoy privileges. Therefore, the room number of VIP guests will be different, so in addition to the basic type name, there must be parentheses, etc. The following is the definition of a function pointer

int*hanrui)(int a,double b)
//以上定义一个名字为hanrui的函数指针,该指针类型为“指向返回int 类型并带有int ,double形参的函数指针”

Be sure to pay attention to the position of the brackets and don’t make a mistake

Usually for convenience, the keyword typedef can be used, so the above definition can be written like this

typedef int (*hanrui)(int a, double b);
//之后在定义可以 hanrui a;就定义了该类型的指针,不必每次都把整个类型声明全部写出来

The following is an example of using function pointers

void test(int a)
{
    
    
	cout << "this is a pointer test" <<a<< endl;
}
int main()
{
    
    
	int a = 1;
	void(*z)(int a);
	z = test;
	z(1);

}

Output results The
Insert picture description here
above are two simple knowledge points

Guess you like

Origin blog.csdn.net/qq_41803340/article/details/108769160