我决定尝试使用 Developing C wrapper API for Object-Oriented C++ code 中描述的模型为 V8 API 创建一个简单的 C 包装器.不幸的是,我不太熟悉 C++,所以我遇到了继承构造函数的问题。
v8capi.h
typedef struct V8Context V8Context;
#ifdef __cplusplus
extern "C" {
#endif
V8Context *V8_NewContext();
#ifdef __cplusplus
}
#endif
v8capi.cpp
#include <v8.h>
struct V8Context : public v8::Handle<v8::Context> { };
V8Context *V8_NewContext() {
v8::HandleScope hscope;
return new V8Context(v8::Context::New());
}
据我了解,new V8Context(...)
应该调用v8::Handle<T>
的构造函数采用 Handle<T>
. v8::Context::New()
返回 v8::Persistent<T>
, 它继承了 v8::Handle<T>
,所以这应该有效。但实际上,它试图调用一个带有 const V8Context &
的构造函数:
error C2664: 'V8Context::V8Context' : cannot convert parameter 1 from
'v8::Persistent<T>' to 'const V8Context &'
with
[
T=v8::Context
]
No user-defined-conversion operator available that can perform this
conversion, or the operator cannot be called
我做错了什么?
最佳答案
V8Context
中没有定义构造函数,因此只有隐式复制构造函数作为选项出现。您需要在 V8Context
中显式定义构造函数转发 v8::Persistent<T>
对其基类的参数。
关于c++ - 无法在从 C++ 类继承的结构上调用正确的构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8839456/