更改C+中的Python对象+;参照

更改C+中的Python对象+;参照,python,c++,pointers,pass-by-reference,boost-python,Python,C++,Pointers,Pass By Reference,Boost Python,问题如下。 比如说,我有一个类Matrix,它通过Boost.Python向Python公开。 现在,这里是另一个类Calcs: class Calcs { public: Matrix* x; Matrix* res; Calcs(){ x = NULL; res = NULL; } void set_x(Matrix& x_){ x = &x_; } void set_res(Matrix& res_){ res = &am

问题如下。 比如说,我有一个类
Matrix
,它通过Boost.Python向Python公开。 现在,这里是另一个类
Calcs

class Calcs
{
public:
    Matrix* x;
    Matrix* res;

    Calcs(){ x = NULL; res = NULL; }

    void set_x(Matrix& x_){ x = &x_; }
    void set_res(Matrix& res_){ res = &res_; }

    void run_calc(){ *res = *x; }
    Matrix get_res(){ return *res; }
};
Python的用法是:

x = Matrix(2,2)  # this will allocate memory for the matrix x
y = Matrix(2,2)  # same for y

c = Calcs() 
c.set_x(x)     # so now the internal pointer of the c object points to the 
               # location of the object x
c.set_res(y)   # same for y
c.run_calc()   # so now, we should be able to look at the object y and find 
               # the result stored in it, but it reality this object 
               # is not actually changed
c.get_res()    # this will give the answer as expected
正如Python脚本的注释所示,使用模式是在外部对象(输入和结果)中分配内存,然后使
Calcs
对象中的指针指向这些位置,运行计算,然后在
y
中准备好结果。但不知怎么的,这不起作用,我很难理解为什么

好的方面是,每当我更新输入矩阵
x
并使用函数
get_res()
返回结果时,结果会根据
x
的值每次更新

不过,矩阵
y
不会更改其内容。我认为Python可能正在创建一些中间对象,所以当我在C++中更新Y时,只有中间对象看到了变化,这就是为什么对象<代码> y>代码>没有更新的原因。p> 所以,我想知道这是否是一个正确的理解,以及如何解决这个问题