C++入门经典-例5.9-使用空类型指针执行函数

1:运行代码:

// 5.9.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <iostream>
using std::cout;
using std::endl;
int plus(int b)
{
    return b+1;
}
int main()
{
    void* pV = NULL;
    int result = 0;
    pV = plus;
    cout<<"执行pV指向的函数:"<<endl;
    result=((int(*)(int))pV)(10);
    cout<<"result:"<<result<<endl;
    return 0;
}
/*首先利用指针调用函数的格式为(*pV)(10),但是pV是空类型指针。使用空类型指针调用自身所指向的函数,应按照强制转换
的形式使用。因此还是记住吧。(int(*)(int))*/
View Code

运行结果:

posted @ 2017-09-15 09:14  一串字符串  阅读(172)  评论(0编辑  收藏  举报