首页 > 解决方案 > 用 C++ 编写的函数在 python 中重载 + 运算符

问题描述

是否可以使用 Python C API 重载 pythons + 运算符。

例如,如果我在 python 中运行此代码:

C = A+B

我希望能够调用这个用 C++ 编写的函数

template<typename T>
QSMatrix<T> QSMatrix<T>::operator+(const QSMatrix<T>& rhs) {
QSMatrix result(rows, cols, 0.0);

for (unsigned i=0; i<rows; i++) {
  for (unsigned j=0; j<cols; j++) {
    result(i,j) = this->mat[i][j] + rhs(i,j);
  }
}

return result;}

这样做可能吗?

标签: pythonc++extending

解决方案


推荐阅读