英文:
The methods `extend` and `implement` are not generated when using `allow_subclass`
问题
I have a C++ class with a number of pure virtual methods. To allow that a JS class can implement those I need to bind the constructor(s) of that class. Unfortunately, embind won't allow that, because of the pure virtual methods.
As an attempt to solve this problem I created a helper C++ class, which implements all those pure virtual methods and returns empty values for them. This leaves me with this code:
class Test {
public:
Test(int i) {}
~Test() {}
virtual std::string get() const = 0;
}
class TestHelper extends Test {
public:
virtual std::string get() const override { return ""; }
}
class TestWrapper : public wrapper<TestHelper> {
public:
EMSCRIPTEN_WRAPPER(TestWrapper);
virtual ~TestWrapper() noexcept override {
}
}
EMSCRIPTEN_BINDINGS(main) {
class_<TestHelper, base<Test>>("Test")
.allow_subclass<TestWrapper>("TestWrapper");
}
This compiles fine and both Test
and TestWrapper
exist in the generated wasm module. However, neither of the two contain the methods extend
and implement
. Other classes, which don't use an intermediate class to implement the pure virtual methods contain those methods.
What is the correct way to generate the missing methods also in the case of the Test
class?
英文:
I have a C++ class with a number of pure virtual methods. To allow that a JS class can implement those I need to bind the constructor(s) of that class. Unfortunately, embind won't allow that, because of the pure virtual methods.
As an attempt to solve this problem I created a helper C++ class, which implements all those pure virtual methods and returns empty values for them. This leaves me with this code:
class Test {
public:
Test(int i) {}
~Test() {}
virtual std::string get() const = 0;
}
class TestHelper extends Test {
public:
virtual std::string get() const override { return ""; }
}
class TestWrapper : public wrapper<TestHelper> {
public:
EMSCRIPTEN_WRAPPER(TestWrapper);
virtual ~TestWrapper() noexcept override {
}
}
EMSCRIPTEN_BINDINGS(main) {
class_<TestHelper, base<Test>>("Test")
.allow_subclass<TestWrapper>("TestWrapper");
}
This compiles fine and both Test
and TestWrapper
exist in the generated wasm module. However, neither of the two contain the methods extend
and implement
. Other classes, which don't use an intermediate class to implement the pure virtual methods contain those methods.
What is the correct way to generate the missing methods also in the case of the Test
class?
答案1
得分: 0
“extend”和“implement”方法只有在整个继承链被绑定时才会被添加。在上述情况下,Test
类没有绑定。一旦它被添加,缺失的方法也会出现。
英文:
The extend
and implement
methods are only added, if the entire inheritance chain is bound. In the scenario above no binding for the Test
class exists. Once it is added, also the missing methods appear.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论