簡體   English   中英

Boost :: python-在python中的類對象上填充指針的C ++向量

[英]Boost::python - filling a C++ vector of pointer on object of a class in python

這是我的問題。 我應該做一個能夠在C ++代碼類的對象上填充指針的C ++向量的python代碼。 我正在使用Boost :: Python。 看起來如下:

C ++代碼:

class A_Base {}
class A_derived_1 {}
...
class A_derived_N {}

class B {
...
setAderivediList(vector<A *> &_AderivedList){}
}

我需要在Python中執行的操作:

B.setAderivediList(_AderivedList)

我無法修改C ++代碼中的方法,只能在#ifdef python_interface之間添加其他對象,以免影響其他開發人員的工作。 剛開始,我嘗試使用我在Internet上找到的轉換器編寫一個轉換器,但未能使它適用於我的代碼。 然后,我嘗試在類B中添加一個_AderivedList和一個填充向量的設置器。 盡管它是用C ++編譯的,但在Python中卻無法使用。

新B類:

class B {
...
setAderivediList(vector<A *> &_AderivedList){}
#ifdef myPython_code
public:
  vector<A *> * _AderivedListPy;
  setAListPy(A * &my_Aderived){       //Actually, I tried without the reference as well
  this->_AderivedListPy->push_back(my_Aderived);
};
#endif
}

在python中變成:

myB = mydll.B()
Ai = mydll.A_derived_i()
myB.setAListPy(Ai) #stopping here
myB.setAderivediList(myB._AderivedListPy)

如果使用引用,它將在myB.setAListPy(Ai)處引發“ python參數類型與C ++簽名不匹配”錯誤消息,如果沒有引用,它將在同一行停止而不引發任何錯誤消息。 關於如何改進此方法或任何其他方法的任何線索? 先感謝您。

實際上,我找到了解決問題的方法。 通過直接在setAListPy中填充向量(包括調用setAderivediList)。 我只是將一個int my_A_derived傳遞給setAListPy(實際上,以及所有A_derived類構造函數所需的其他arg),然后在my_A_derived上使用if條件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM