如何从此 v8 class 正确创建派生 class
How to properly create a derived class from this v8 class
所以我有一个 v8 中的 class,我希望我自己的 class 从中派生,然后实例化我自己的 class.
的一个实例
在 v8 中有 class 带有这个签名:
class V8_EXPORT CodeEventHandler {
public:
/**
* Creates a new listener for the |isolate|. The isolate must be initialized.
* The listener object must be disposed after use by calling |Dispose| method.
* Multiple listeners can be created for the same isolate.
*/
explicit CodeEventHandler(Isolate* isolate);
virtual ~CodeEventHandler();
/**
* Handle is called every time a code object is created or moved. Information
* about each code event will be available through the `code_event`
* parameter.
*
* When the CodeEventType is kRelocationType, the code for this CodeEvent has
* moved from `GetPreviousCodeStartAddress()` to `GetCodeStartAddress()`.
*/
virtual void Handle(CodeEvent* code_event) = 0;
/**
* Call `Enable()` to starts listening to code creation and code relocation
* events. These events will be handled by `Handle()`.
*/
void Enable();
/**
* Call `Disable()` to stop listening to code creation and code relocation
* events.
*/
void Disable();
private:
CodeEventHandler();
CodeEventHandler(const CodeEventHandler&);
CodeEventHandler& operator=(const CodeEventHandler&);
void* internal_listener_;
};
我自己的派生 class 看起来像这样:
class CodeEventHandler : public v8::CodeEventHandler
{
public:
~CodeEventHandler()
{
listeners.clear();
}
void Handle(v8::CodeEvent* event)
{
// code omitted for readibility
}
v8::Local<v8::Object> SerializeEvent(v8::CodeEvent* event)
{
// code omitted for readibility
}
void AddListener(v8::Local<v8::Function> listener)
{
// code omitted for readibility
}
void SetResource(CV8ResourceImpl* _resource)
{
resource = _resource;
}
private:
CV8ResourceImpl* resource;
std::vector<v8::UniquePersistent<v8::Function>> listeners;
};
但是当我想像这样实例化我的 class 时:
v8::Isolate* isolate = v8::Isolate::GetCurrent();
auto codeEventHandler = new CodeEventHandler(isolate);
我收到这个错误:
error C2664: 'CodeEventHandler::CodeEventHandler(const CodeEventHandler &)': cannot convert argument 1 from 'v8::Isolate *' to 'const CodeEventHandler &'
我做错了什么?
(这不是特定于 V8 的问题;它是普通的 C++。)
正如@JosephLarson 指出的那样,您需要一个具有正确签名的构造函数。将此添加到您的 class:
explicit CodeEventHandler(Isolate* isolate)
: v8::CodeEventHandler(isolate) {
// any code you need to initialize your subclass instances...
}
所以我有一个 v8 中的 class,我希望我自己的 class 从中派生,然后实例化我自己的 class.
的一个实例在 v8 中有 class 带有这个签名:
class V8_EXPORT CodeEventHandler {
public:
/**
* Creates a new listener for the |isolate|. The isolate must be initialized.
* The listener object must be disposed after use by calling |Dispose| method.
* Multiple listeners can be created for the same isolate.
*/
explicit CodeEventHandler(Isolate* isolate);
virtual ~CodeEventHandler();
/**
* Handle is called every time a code object is created or moved. Information
* about each code event will be available through the `code_event`
* parameter.
*
* When the CodeEventType is kRelocationType, the code for this CodeEvent has
* moved from `GetPreviousCodeStartAddress()` to `GetCodeStartAddress()`.
*/
virtual void Handle(CodeEvent* code_event) = 0;
/**
* Call `Enable()` to starts listening to code creation and code relocation
* events. These events will be handled by `Handle()`.
*/
void Enable();
/**
* Call `Disable()` to stop listening to code creation and code relocation
* events.
*/
void Disable();
private:
CodeEventHandler();
CodeEventHandler(const CodeEventHandler&);
CodeEventHandler& operator=(const CodeEventHandler&);
void* internal_listener_;
};
我自己的派生 class 看起来像这样:
class CodeEventHandler : public v8::CodeEventHandler
{
public:
~CodeEventHandler()
{
listeners.clear();
}
void Handle(v8::CodeEvent* event)
{
// code omitted for readibility
}
v8::Local<v8::Object> SerializeEvent(v8::CodeEvent* event)
{
// code omitted for readibility
}
void AddListener(v8::Local<v8::Function> listener)
{
// code omitted for readibility
}
void SetResource(CV8ResourceImpl* _resource)
{
resource = _resource;
}
private:
CV8ResourceImpl* resource;
std::vector<v8::UniquePersistent<v8::Function>> listeners;
};
但是当我想像这样实例化我的 class 时:
v8::Isolate* isolate = v8::Isolate::GetCurrent();
auto codeEventHandler = new CodeEventHandler(isolate);
我收到这个错误:
error C2664: 'CodeEventHandler::CodeEventHandler(const CodeEventHandler &)': cannot convert argument 1 from 'v8::Isolate *' to 'const CodeEventHandler &'
我做错了什么?
(这不是特定于 V8 的问题;它是普通的 C++。)
正如@JosephLarson 指出的那样,您需要一个具有正确签名的构造函数。将此添加到您的 class:
explicit CodeEventHandler(Isolate* isolate)
: v8::CodeEventHandler(isolate) {
// any code you need to initialize your subclass instances...
}