pio*_*otr 5 python boost pointers arguments argument-passing
使用指针作为参数的函数使用boost python的最佳方法是什么?我看到文档中有很多返回值的可能性,但我不知道如何用参数来做.
void Tesuto::testp(std::string* s)
{
if (!s)
cout << " NULL s" << endl;
else
cout << s << endl;
}
>>> t.testp(None)
NULL s
>>>
>>> s='test'
>>> t.testp(s)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
Boost.Python.ArgumentError: Python argument types in
Tesuto.testp(Tesuto, str)
did not match C++ signature:
testp(Tesuto {lvalue}, std::string*)
>>>
Run Code Online (Sandbox Code Playgroud)
据我所知,在对这个主题进行了一些谷歌搜索之后,你不能。Python 默认不支持指针参数类型。如果您愿意,您可能可以手动编辑 python 解释器,但在我看来,这似乎是某种生产代码,因此这可能不是一个选择。
编辑:您可以添加一个包装函数,如下所示:
std::string * pointer (std::string& p)
{
return &p;
}
然后使用以下命令调用您的代码:
>>> s = 'hello'
>>> t.testp (pointer (s))
hello
>>>
Run Code Online (Sandbox Code Playgroud)