在Python中重载+运算符,用C++编写函数

2024-10-04 07:24:44 发布

您现在位置:Python中文网/ 问答频道 /正文

是否可以使用pythoncapi重载pythons+操作符

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

C = A+B

ID,能够调用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;}

这样做可能吗


Tags: 函数代码idfortemplateresultoperatorrows