python - boost python的指针参数

标签 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*)
>>>                        

最佳答案

据我所知,在对这个主题进行了一些谷歌搜索后,你不能。默认情况下,Python 不支持指针参数类型。如果您愿意,您可能可以手动编辑 Python 解释器,但在我看来这似乎是某种生产代码,所以这可能不是一个选项。

编辑:您可以像这样添加一个包装函数:

 
std::string * pointer (std::string& p)
{
    return &p;
}

Then call your code with:


>>> s = 'hello'
>>> t.testp (pointer (s))
hello
>>>

关于python - boost python的指针参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2385561/

相关文章:

c++ - boost program options 一个选项的多个值

c - C 中使用指针数组或指向指针的指针的二维数组?

python - 使用 os.system 重新启动程序以节省内存?

python - 在 Jinja2 中,如何结合 block 标签使用宏?

c++ - 在 cmd pmt 中执行带空格的文件名 从 C++ 程序传递

c - 动态分配二维数组以匹配 LINES 和 COLS

pointers - go:使用指针允许更改结构的内容。为什么?

python - Django `with` 标签无法识别关键字参数

python - 在 Windows 10 中获取启动时间并不总是相同的时间

c++ - 如何处理大数?