2010-03-05 50 views
5

使用boost python指針作爲參數的函數的最佳方法是什麼? 我發現文檔中有很多返回值的可能性,但我不知道如何用參數來做。指針參數來提升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

據我所知,在做了一些關於該主題的搜索之後,你不能這樣做。默認情況下,Python不支持指針參數類型。如果你想,你可以手工編輯python解釋器,但這在我看來似乎是某種類型的生產代碼,所以這可能不是一種選擇。

編輯:你可以添加一個包裝函數,像這樣:

 

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

然後打電話給你的代碼:

 

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

+0

通過在Boost.Python的一個參考一個問題,我們只能通過const引用,除非實現我們自己的引用包裝,你有什麼想法來實現這個嗎? – 2014-11-21 01:03:17