2021.07.23牛客

回调函数

回调函数可以把函数指针作为参数传到另一个函数,就是在特定的事件发生后我们要调用这个实现,作用可能就是解耦。

#include<stdio.h>

int Callback_1(int x) // Callback Function 1
{
    printf("Hello, this is Callback_1: x = %d ", x);
    return 0;
}

int Callback_2(int x) // Callback Function 2
{
    printf("Hello, this is Callback_2: x = %d ", x);
    return 0;
}

int Callback_3(int x) // Callback Function 3
{
    printf("Hello, this is Callback_3: x = %d ", x);
    return 0;
}

int Handle(int y, int (*Callback)(int))
{
    printf("Entering Handle Function. ");
    Callback(y);
    printf("Leaving Handle Function. ");
}

int main()
{
    int a = 2;
    int b = 4;
    int c = 6;
    printf("Entering Main Function. ");
    Handle(a, Callback_1);
    Handle(b, Callback_2);
    Handle(c, Callback_3);
    printf("Leaving Main Function. ");
    return 0;
}

运行结果:

Entering Main Function.
Entering Handle Function.
Hello, this is Callback_1: x = 2
Leaving Handle Function.
Entering Handle Function.
Hello, this is Callback_2: x = 4
Leaving Handle Function.
Entering Handle Function.
Hello, this is Callback_3: x = 6
Leaving Handle Function.
Leaving Main Function.

指针常量,常量指针

const出现在左边,如const char p,表示p所指向的变量内容不可变,指针指向可以改变; 指针常量
const出现在右边,如char const p,表示p是个常量指针,即不能指向其他变量,而指向的变量内容可变; const出现在左边和右边,如const char const p,表示p的指向不能改变,指向的变量内容也不能改变。

static类型的变量,默认的初始化值是 0。(例如: 浮点型,整型....)。字符型和字符数组, 初始化值是 空,'\0' 。

多次重复fclose一个打开过的问及那描述符会出错 ,就等于多次free指针。

C++规定=,[ ],(),->这四个运算符只能被重载为类的非静态成员函数,其他的可以被友元重载

主要是因为其他的运算符重载函数都会根据参数类型或数目进行精确匹配,这四个不具有这种检查的功能,用友元定义就会出错

switch后面跟的只能是int,char,枚举型,别的不行。

posted @ 2021-07-23 19:56  爱吃鱼的小女孩  阅读(33)  评论(0编辑  收藏  举报