cppyy继承包含智能指针的类

问题描述 投票:2回答:1

这是从包含智能指针的类继承的简单示例。我们对此不做任何事情,只需声明一下即可。

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 ++类访问公共变量。实际上,我不确定100%cppyy是否在这里出错。尽管C ++看起来还不错,但我可能以一种奇怪的方式使用了智能指针/虚拟析构函数,因为我对智能指针并不了解。

如果我使用std::shared_ptr而不是std::unique_ptr,则不会引发错误>

这是从包含智能指针的类继承的简单示例。我们对此不做任何事情,只需声明它即可。 import cppyy cppyy.cppdef(“”“类示例{private:std:...

python c++ smart-pointers cppyy
1个回答
1
投票

正如S.M.所暗示的,如果我们必须使用unique_ptr,技巧似乎是确保定义一个副本构造函数,例如,此示例给出了没有错误消息的预期结果,

© www.soinside.com 2019 - 2024. All rights reserved.