python与c++;两者不相同

python与c++;两者不相同,python,c++,pybind11,Python,C++,Pybind11,我尝试用pybDun11在C++和python中打印同一对象的内存地址,但是我发现从两个内存地址返回的都不相同。 c++端 class Example { public: Example() {std::cout << "constuctor" << this << std::endl;} ~Example() {std::cout << "destructor " << this << std::end

我尝试用pybDun11在C++和python中打印同一对象的内存地址,但是我发现从两个内存地址返回的都不相同。 c++端

class Example {
  public:
    Example() {std::cout << "constuctor" << this << std::endl;}
    ~Example() {std::cout << "destructor " << this << std::endl;}
};

class ABC {
  public:
    static std::unique_ptr<Example> get_example() {
      // std::shared_ptr<Example> ptr = std::make_shared<Example>();
      std::unique_ptr<Example> ptr = std::make_unique<Example>();
      return ptr;
    }
};

void init_example(py::module & m) {
    py::class_<ABC>(m, "ABC")
    .def_static("get_example", &ABC::get_example);
}
输出

constuctor0x234cd80
<Example object at 0x7f5493c37928>
destructor 0x234cd80
constructor 0x234cd80
析构函数0x234cd80
< > C++内存地址为0x23 4CD80,但Python为0x7F54 93C3928


我想知道这个问题的Python方面,但是关注C++部分,你没有打印出正确的信息。

std::unique_ptr
与创建的
示例
实例具有不同的地址,因此值将不同。如果要打印
unique\u ptr
正在寻址的项目的地址,需要调用
get()
函数

下面是一个完整的示例,显示了不同之处:

#include <memory>
#include <iostream>

class Example {
  public:
    Example() {std::cout << "constuctor " << this << std::endl;}
    ~Example() {std::cout << "destructor " << this << std::endl;}
};

class ABC {
  public:
    static std::unique_ptr<Example> get_example() 
    {
      std::unique_ptr<Example> ptr = std::make_unique<Example>();
      return ptr;
    }
};

int main()
{
    std::unique_ptr<Example> p = ABC::get_example();
    std::cout << "The unique_ptr address is: " << &p << std::endl;
    std::cout << "The get() function returns: " << p.get() << std::endl;
}

你需要调整你的python代码来打印返回值<代码> GET()/<代码> ./p> < p>我不熟悉这个问题的Python方面,但是关注C++部分,你没有打印正确的信息。
std::unique_ptr
与创建的
示例
实例具有不同的地址,因此值将不同。如果要打印
unique\u ptr
正在寻址的项目的地址,需要调用
get()
函数

下面是一个完整的示例,显示了不同之处:

#include <memory>
#include <iostream>

class Example {
  public:
    Example() {std::cout << "constuctor " << this << std::endl;}
    ~Example() {std::cout << "destructor " << this << std::endl;}
};

class ABC {
  public:
    static std::unique_ptr<Example> get_example() 
    {
      std::unique_ptr<Example> ptr = std::make_unique<Example>();
      return ptr;
    }
};

int main()
{
    std::unique_ptr<Example> p = ABC::get_example();
    std::cout << "The unique_ptr address is: " << &p << std::endl;
    std::cout << "The get() function returns: " << p.get() << std::endl;
}

你需要调整你的python代码来打印返回代码值<代码> GET()/Cyto>。< /P> < P> ByBID11创建一个Python对象,该对象具有C++对象的引用。所以Python pybDn11包装器和C++对象的地址不同。p>

默认PybDn11 STR对象表示中的地址是Python对象的地址,不是底层C++对象或智能指针的地址。

如果你需要知道C++对象的地址,就添加一个方法来绑定代码,如@ PaulMcKunZi所建议的。 C++:

输出:

constuctor 0x555a68bd7c20
The unique_ptr address is: 0x7ffd9fa6c120
The get() function returns: 0x555a68bd7c20
destructor 0x555a68bd7c20
构造函数0x10eff20
C++地址:10Eff20
析构函数0x10eff20

< /C> > p> pybd11创建一个Python对象,该对象具有C++对象的引用。所以Python pybDn11包装器和C++对象的地址不同。p>

默认PybDn11 STR对象表示中的地址是Python对象的地址,不是底层C++对象或智能指针的地址。

如果你需要知道C++对象的地址,就添加一个方法来绑定代码,如@ PaulMcKunZi所建议的。 C++:

输出:

constuctor 0x555a68bd7c20
The unique_ptr address is: 0x7ffd9fa6c120
The get() function returns: 0x555a68bd7c20
destructor 0x555a68bd7c20
构造函数0x10eff20
C++地址:10Eff20
析构函数0x10eff20

我不太明白您要打印的内容。在C++代码中打印出<代码>此< /代码>,但实际上在<代码> GETSub示例< /C>中,您创建了<代码> STD::UnQuyJPPT,其地址不与<>代码> < <代码> >。这个问题似乎和Python无关。我的错误。我认为
std::unique_ptr
的地址与它指向的对象的地址相同。但是如何在python中打印出对象的内存地址呢?你需要调用
get()
函数,不管python给你什么样的方法。我不太明白你想要打印出什么。在C++代码中打印出<代码>此< /代码>,但实际上在<代码> GETSub示例< /C>中,您创建了<代码> STD::UnQuyJPPT,其地址不与<>代码> < <代码> >。这个问题似乎和Python无关。我的错误。我认为
std::unique_ptr
的地址与它指向的对象的地址相同。但是如何在python中打印出对象的内存地址呢?你需要调用
get()
函数,不管python给你什么方法。我做了一些更改,但返回的内存地址仍然不相同<代码> > UNQuijpTrpAdvest= 0x7FCFC78CDA70< /Cord>,<代码> python·SydAdAddie= 0x7FF4C80AB8F0 >“因此,您需要调整Python代码来打印GET()的返回值。”或者“调整C++代码,”?我做了一些更改,但返回的内存地址仍然不完全相同。代码> UNQuijpTrpAdvest= 0x7FCFC78CDA70< /COD>,<代码> PythONSythAddioTeal= 0x7FF4C80AB8F0 >“因此,您需要调整Python代码来打印GET()的返回值),或者“调整C++代码”,而不是?
constuctor 0x10eff20
<example_module.Foo object at 0x7f51c71d4298>
C++ address: 10eff20
destructor 0x10eff20