c pass function as function parameter

#include <iostream>
using namespace std;

void printNum(int x);
void invokeFunc2(void (*funcName)(int));

int main()
{ 
    invokeFunc2(&printNum);
    return 0;
}

void invokeFunc2(void (*funcName)(int))
{
    int x=100;
    (*funcName)(x);
}

void printNum(int x)
{
    for(int i=0;i<100;i++)
    {
        cout<<"x="<<++x<<endl;
        sleep(1);
    }

    cout<<"Finished in printNum()!"<<endl;
}

 

posted @ 2021-12-16 18:06  FredGrit  阅读(22)  评论(0编辑  收藏  举报