5

使具有指针作为参数的函数与 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*)
>>>                        
4

1 回答 1

4

As far as I can tell, after doing a bit of googling on the subject, is that you can't. Python doesn't support pointer argument types by default. If you wanted to, you could probably edit the python interpreter by hand, but this seems to me to be production code of some sort, so that probably isn't an option.

EDIT: You could add a wrapper function like so:

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

Then call your code with:


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

于 2010-03-27T23:43:25.250 回答