关键字 Typedef
关键字 Typedef
[TOC]
typedef
//2024年2月6日-HSB 待补充
示例1:成员函数指针
typedef 实例
TKClassFunctor.h 中的源码:
1
2
3
4
5
6
template<class CS, class ret_type, class arg_type>
class function_impl : public function_base<ret_type, arg_type>
{
typedef ret_type (CS::* PROC)(arg_type);
// ...
};
代码释义:
ret_type: 函数的 返回值 类型。CS::*: 表示这是一个指向CS类内部成员 的指针。PROC: 这是我们定义的 新类型名称(可以理解为 “Procedure” 的缩写)。(arg_type): 表示该函数接受一个类型为arg_type的 参数。
typedef 总结:定义 PROC 为一种指针类型,它指向 CS 类中的某个成员函数,该函数接受一个 arg_type 类型的参数,并返回 ret_type 类型的值。
using 代替方案
如果想在类外面单独定义这个类型,需要使用 C++11 引入的 using 语法:
1
2
template<class CS, class ret_type, class arg_type>
using PROC = ret_type (CS::*)(arg_type);
这样 PROC 就变成了一个独立的类型别名模板,可以在别处直接像 PROC<MyClass, void, int> 这样使用。
参考文章1234
本文由作者按照
CC BY 4.0
进行授权