C++中的typedef


typedef

几个用途:

  • 定义类型的别名

    typedef char* PCHAR

  • typedef + struct 定义结构体,其实与别名本质上是一样的

    typedef struct {
        char* name;
        int age;
        int gender;
        char* address;
    } Person;
    
    // person是一个变量
    Person person;
  • 函数指针

    一般来说,定义函数指针并不需要使用typedef,但是使用typedef会更加的直观:

    #include <iostream>
    
    typedef int (*ComputeSum)(int a, int b);
    
    ComputeSum sum;
    
    int sumFunc(int a, int b) {
       return a + b;
    }
    
    int main() {
        sum = sumFunc;
        int result = sum(1,2);
        std::cout << result << std::endl;
        return 0;
    }

    函数指针的格式: 返回类型 (*函数名字)(形式参数);

    再举一个实际点的例子吧(Flutter Engine中的一段):

    在embedder.h中有:

    typedef FlutterEngineResult (*FlutterEngineOnVsyncFnPtr)(
        FLUTTER_API_SYMBOL(FlutterEngine) engine,
        intptr_t baton,
        uint64_t frame_start_time_nanos,
        uint64_t frame_target_time_nanos);
    
    FlutterEngineOnVsyncFnPtr OnVsync;

    在embedder.cc中有实现:

    #define SET_PROC(member, function)        \
      if (STRUCT_HAS_MEMBER(table, member)) { \
        table->member = &function;            \
      }
    
    SET_PROC(OnVsync, FlutterEngineOnVsync);
    
    FlutterEngineResult FlutterEngineOnVsync(FLUTTER_API_SYMBOL(FlutterEngine)
                                                 engine,
                                             intptr_t baton,
                                             uint64_t frame_start_time_nanos,
                                             uint64_t frame_target_time_nanos) {
      if (engine == nullptr) {
        return LOG_EMBEDDER_ERROR(kInvalidArguments, "Invalid engine handle.");
      }
    
      TRACE_EVENT0("flutter", "FlutterEngineOnVsync");
    
      auto start_time = fml::TimePoint::FromEpochDelta(
          fml::TimeDelta::FromNanoseconds(frame_start_time_nanos));
    
      auto target_time = fml::TimePoint::FromEpochDelta(
          fml::TimeDelta::FromNanoseconds(frame_target_time_nanos));
    
      if (!reinterpret_cast<flutter::EmbedderEngine*>(engine)->OnVsyncEvent(
              baton, start_time, target_time)) {
        return LOG_EMBEDDER_ERROR(
            kInternalInconsistency,
            "Could not notify the running engine instance of a Vsync event.");
      }
    
      return kSuccess;
    }
    
    FlutterEngineResult FlutterEngineReloadSystemFonts(
        FLUTTER_API_SYMBOL(FlutterEngine) engine) {
      if (engine == nullptr) {
        return LOG_EMBEDDER_ERROR(kInvalidArguments, "Invalid engine handle.");
      }
    
      TRACE_EVENT0("flutter", "FlutterEngineReloadSystemFonts");
    
      if (!reinterpret_cast<flutter::EmbedderEngine*>(engine)
               ->ReloadSystemFonts()) {
        return LOG_EMBEDDER_ERROR(kInternalInconsistency,
                                  "Could not reload system fonts.");
      }
    
      return kSuccess;
    }

文章作者: 姜康
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 姜康 !
评论
 上一篇
C++11中的std::function C++11中的std::function
先来看一个例子: #include <iostream> std::function<int(int, int)> func; int add(int a, int b) { return a + b; } aut
2020-12-06
下一篇 
Golang-处理json Golang-处理json
Marshalmarshal的中文意思是“编码”,“数据打包”的意思 基本操作编码json.Marshal(v interface{}) ([]byte, error) 即将一个struct对象序列化成字节数组. 解码json.Unmars
2020-12-02