Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/126.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何复制PyObject*? 我从下面的C++函数调用python函数。< /p> void CPPFunction(PyObject* pValue) { ... pValue = PyObject_CallObject(PythonFunction, NULL); ... } int main() { PyObject *pValue = NULL; CPPFunction(PValue); int result_of_python_function = Pylong_aslong(PValue); }_Python_C++_Python Embedding - Fatal编程技术网

如何复制PyObject*? 我从下面的C++函数调用python函数。< /p> void CPPFunction(PyObject* pValue) { ... pValue = PyObject_CallObject(PythonFunction, NULL); ... } int main() { PyObject *pValue = NULL; CPPFunction(PValue); int result_of_python_function = Pylong_aslong(PValue); }

如何复制PyObject*? 我从下面的C++函数调用python函数。< /p> void CPPFunction(PyObject* pValue) { ... pValue = PyObject_CallObject(PythonFunction, NULL); ... } int main() { PyObject *pValue = NULL; CPPFunction(PValue); int result_of_python_function = Pylong_aslong(PValue); },python,c++,python-embedding,Python,C++,Python Embedding,我想在cpp函数之外访问python函数的返回值。由于PyObject_CallObject返回的POObject*的作用域在CPPFunction内,如何访问CPPFunction外的值?像在其他任何地方一样从函数返回它 PyObject* CPPFunction() { // ... PyObject* pValue = PyObject_CallObject(PythonFunction, NULL); // ... return pValue; } in

我想在cpp函数之外访问python函数的返回值。由于PyObject_CallObject返回的POObject*的作用域在CPPFunction内,如何访问CPPFunction外的值?

像在其他任何地方一样从函数返回它

PyObject* CPPFunction()
{
    // ...
    PyObject* pValue = PyObject_CallObject(PythonFunction, NULL);
    // ...
    return pValue;
}

int main()
{
  PyObject *value = CPPFunction();
  int result_of_python_function = Pylong_aslong(value);
}

进行以下更改,您可以在CPP函数之外访问python函数的返回值。希望这对您有所帮助:

PyObject* CPPFunction(PyObject* PythonFunction) // changes return type from void to PyObject and pass PythonFunction to be called
{
  pValue = PyObject_CallObject(PythonFunction, NULL);
  return pValue;
}

int main()
{
   PyObject *pValue = NULL;
   pValue = CPPFunction(PythonFunction); // assign return value from CPPFunction call to PyObject pointer pvalue
   long int result_of_python_function = Pylong_aslong(PValue);// data type changed from int to long int
   cout << result_of_python_function << endl; // just printing the python result
}
PyObject*CPPFunction(PyObject*PythonFunction)//将返回类型从void更改为PyObject,并传递要调用的PythonFunction
{
pValue=PyObject\u CallObject(PythonFunction,NULL);
返回pValue;
}
int main()
{
PyObject*pValue=NULL;
pValue=CPPFunction(PythonFunction);//将CPPFunction调用的返回值赋给PyObject指针pValue
long int result_of_python_function=Pylong_aslong(PValue);//数据类型从int更改为long int

无法将地址保存在类成员中?或者错误的方法:使用全局…(请不要)PyObject\u CallObject函数返回的任何对象仅在CPP函数中具有生存期。在这种情况下,无论是返回还是将其传递给全局变量,它都不会进行任何更改。我们无法访问它。对吗?我们可以通过返回对象内存位置作为指针并在主函数中创建新指针来访问它o相同的内存位置。你的代码和我的代码有什么区别?两者都是相同的,不是吗?@Kumar不,它们不是。
void CPPFunction(PyObject*pValue)
PyObject*CPPFunction()不同
。我怀疑PyObject\u CallObject返回的值在CPPFunction中有一个生存期。因此,即使我们将其复制到指针或返回它,我们也无法访问其作用域之外的变量。不是吗?@Kumar返回指针所存储的变量的生存期仅限于
CPPFunction
的作用域。如果不销毁,则不会它指向的对象,您可以安全地返回指向该对象的指针,即返回指针的副本。