nodejs c++ h声明结构体
在 Node.js 中使用 C++ 编写扩展时,可以通过以下方式在 .h 文件中声明结构体:
#ifndef MY_STRUCT_H
#define MY_STRUCT_H
typedef struct MyStruct {
int id;
char name[20];
} MyStruct;
#endif
其中,MyStruct 是结构体类型的名称,包含两个成员变量 id 和 name,可以根据实际需要进行修改。
在 .cpp 文件中可以通过以下方式使用该结构体:
#include "my_struct.h"
Napi::Object CreateObject(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
MyStruct myStruct = { 1, "Test" };
Napi::Object obj = Napi::Object::New(env);
obj.Set("id", myStruct.id);
obj.Set("name", myStruct.name);
return obj;
}
在上面的例子中,通过 #include "my_struct.h" 引入结构体类型的声明,并创建了一个 MyStruct 类型的变量 myStruct,最终将其包装成一个 Napi::Object 类型的对象返回
原文地址: https://www.cveoy.top/t/topic/fJVZ 著作权归作者所有。请勿转载和采集!