typedef int(*foo_type)(int); //函数类型由函数返回值类型和参数列表类型决定 typedef std::map foo_map_type; foo_map_type map; map["aaa"] = use_foo1_1; map["bbb"] = use_foo1_2; std::cout << (*map["aaa"])(10) << std::endl; std::cout << (*map["bbb"])(10) << std::endl; //由于 map的value部分只能接受一种类型 // use_foo1_1 和 use_foo2_1 不是同一种类型的函数 //所以 保存函数的指针 使用 void* 这样就能存储了, // 但是通过函数指针调用函数的时候需要把 void* 转换成相对应的指针类型才行 //所以 一个map结点需要保存2个内容 函数指针和 转换函数的类型说明符号 void test2(void) typedef int(*foo1_type)(int); //函数类型由函数返回值类型和参数列表类型决定 typedef int(*foo2_type)(int, int); typedef std::map > foo_map_type; foo_map_type map; map["aaa"] = std::pair(use_foo1_1, 1); map["bbb"] = std::pair(use_foo2_1, 2); if (map["aaa"].second == 1) std::cout << (*reinterpret_cast(map["aaa"].first))(10) << std::endl; if (map["aaa"].second == 2) std::cout << (*reinterpret_cast(map["aaa"].first))(10, 20) << std::endl; if (map["bbb"].second == 1) std::cout << (*reinterpret_cast(map["bbb"].first))(10) << std::endl; if (map["bbb"].second == 2) std::cout << (*reinterpret_cast(map["bbb"].first))(10, 20) << std::endl; 参考C++ 中通过函数名字的字符串调用函数C++ 根据字符串 调用同名函数#include <iostream>#include <map>#include <string>using namespace std;int use_foo1_1(int n){ return n;}int use_foo1_2(int n){ retu... int a=5,sum; double b=5.55; sum=a+b; //隐式类型 换:编译系统首先将a的值由int 换为double,然后与b相加得到10.55,在向整形变量sum赋值时,将10.55 换为整形数10,赋值给变量sum; cout&lt;&lt;"隐式 换:a+b="&l...
文章目录一、string的声明二、string的重载的操作符三、最重要的一个成员 函数 四、string特性描述 函数 五、string的其它成员 函数 六、string的本质七、应用经验八、课后作业九、版权声明 在C语言中,用0结束的字符数组表示 字符串 ,有些不方便:1)数组定义后大小不能改变;2)存入的内容只能比数组小,不能大,如果不小心存多了,会引起内存的溢出,这些问题让程序员有些郁闷。 C++的stri...