cppyy inherit class 包含一个智能指针
cppyy inherit class that contains a smart pointer
这是一个继承自包含智能指针的 class 的简单示例。我们不对它做任何事情,只是声明它。
import cppyy
cppyy.cppdef("""
class Example {
private:
std::unique_ptr<double> x;
public:
Example() {}
virtual ~Example() = default;
double y = 66.;
};
""")
class Inherit(cppyy.gbl.Example):
pass
a = Inherit()
print(a.y) # Test whether this attribute was inherited
示例运行,但出现智能指针错误
input_line_19:9:43: error: call to implicitly-deleted copy constructor of '::Example'
Dispatcher1(const Dispatcher1& other) : Example(other), m_self(other.m_self, this) {}
^ ~~~~~
input_line_17:4:29: note: copy constructor of 'Example' is implicitly deleted because field 'x' has a deleted copy constructor
std::unique_ptr<double> x;
^
/usr/include/c++/7/bits/unique_ptr.h:383:7: note: 'unique_ptr' has been explicitly marked deleted here
unique_ptr(const unique_ptr&) = delete;
^
smart_ptr.py:14: RuntimeWarning: no python-side overrides supported
class Inherit(cppyy.gbl.Example):
66.0
尽管如此,继承似乎起作用了,因为我们仍然可以从 C++ class 访问 public 变量。实际上,我不是 100% 确定 cppyy 是否有问题。虽然 C++ 看起来不错,但我可能以一种奇怪的方式使用智能 pointers/the 虚拟析构函数,因为我对智能指针的使用经验不多。
如果我使用 std::shared_ptr
而不是 std::unique_ptr
,则不会引发错误
正如 S.M 所暗示的那样,如果我们必须使用 unique_ptr
,诀窍似乎是确保定义一个复制构造函数,例如,这个例子给出了预期的结果而没有错误消息,
import cppyy
cppyy.cppdef("""
class Example {
std::unique_ptr<double> x;
public:
Example() { x = std::unique_ptr<double>(new double(123.)); }
// Copy constructor
Example(const Example& other) : x(other.x ? nullptr : new double(*other.x)) {}
virtual ~Example() = default;
double y = 66.;
double get_x() { return *x; }
};
auto e = Example();
auto f = e;
""")
class Inherit(cppyy.gbl.Example):
pass
a = Inherit()
print(a.get_x()) # prints 123.
print(a.y) # prints 66.
这是一个继承自包含智能指针的 class 的简单示例。我们不对它做任何事情,只是声明它。
import cppyy
cppyy.cppdef("""
class Example {
private:
std::unique_ptr<double> x;
public:
Example() {}
virtual ~Example() = default;
double y = 66.;
};
""")
class Inherit(cppyy.gbl.Example):
pass
a = Inherit()
print(a.y) # Test whether this attribute was inherited
示例运行,但出现智能指针错误
input_line_19:9:43: error: call to implicitly-deleted copy constructor of '::Example'
Dispatcher1(const Dispatcher1& other) : Example(other), m_self(other.m_self, this) {}
^ ~~~~~
input_line_17:4:29: note: copy constructor of 'Example' is implicitly deleted because field 'x' has a deleted copy constructor
std::unique_ptr<double> x;
^
/usr/include/c++/7/bits/unique_ptr.h:383:7: note: 'unique_ptr' has been explicitly marked deleted here
unique_ptr(const unique_ptr&) = delete;
^
smart_ptr.py:14: RuntimeWarning: no python-side overrides supported
class Inherit(cppyy.gbl.Example):
66.0
尽管如此,继承似乎起作用了,因为我们仍然可以从 C++ class 访问 public 变量。实际上,我不是 100% 确定 cppyy 是否有问题。虽然 C++ 看起来不错,但我可能以一种奇怪的方式使用智能 pointers/the 虚拟析构函数,因为我对智能指针的使用经验不多。
如果我使用 std::shared_ptr
而不是 std::unique_ptr
正如 S.M 所暗示的那样,如果我们必须使用 unique_ptr
,诀窍似乎是确保定义一个复制构造函数,例如,这个例子给出了预期的结果而没有错误消息,
import cppyy
cppyy.cppdef("""
class Example {
std::unique_ptr<double> x;
public:
Example() { x = std::unique_ptr<double>(new double(123.)); }
// Copy constructor
Example(const Example& other) : x(other.x ? nullptr : new double(*other.x)) {}
virtual ~Example() = default;
double y = 66.;
double get_x() { return *x; }
};
auto e = Example();
auto f = e;
""")
class Inherit(cppyy.gbl.Example):
pass
a = Inherit()
print(a.get_x()) # prints 123.
print(a.y) # prints 66.