c++ - 无法在从 C++ 类继承的结构上调用正确的构造函数

标签 c++ c visual-c++ constructor wrapper

我决定尝试使用 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/

相关文章:

C++ : Get a child member from the Father class

c++ - Qt - 更新 PaintGL

c++ - 多源 .cpp 文件问题

c++ - WIN32 :C++, 为什么在WM_CLOSE上调用Messagebox函数 程序死机

c++ - C/C++ : Converting hexadecimal value in char to integer

c++ - 自动在头文件中添加#pragma once

c - #if 将 undefined symbol 常量假定为 0 是标准的吗?

c++ - C头文件可以被视为接口(interface)吗?

c - 如何触发内存范围访问异常?

visual-studio-2010 - 编译失败-程序数据库