type
status
date
slug
summary
tags
category
icon
password

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; }
CMake的简单使用说明C++中的Lambda
姜康
姜康
一个软件工程师
公告
type
status
date
slug
summary
tags
category
icon
password
🎉博客网站重新制作了🎉
👏欢迎更新体验👏