我的代碼中存在以下情況,並且不知道它是否顯示了一些錯誤的設計。將此變量傳遞給此類變量的方法
class A
{
public:
...
void estimateParameters(const std::vector<double> &data);
private:
B m_algorithm;
int m_parameter1;
int m_parameter2;
};
void A::estimateParameters(const sdt::vector<double> &data)
{
m_algorithm.estimate(this, data); // And then m_parameter1 and m_parameter2 are filled
}
的estimateParameters
該實現調用的B
的estimate
方法,它接收作爲參數的指針this
。這聽起來有點奇怪,似乎多餘。我正在使用此方案封裝參數估計A
,而不強制用戶在其代碼中手動創建B
的實例,然後通過A
。這是一種對用戶進行透明參數估計的方法。
這種方案很常見嗎?你認爲這是做這件事的最好方法嗎?任何替代品不多或更清晰?
謝謝
不應該是'm_algorithm.estimate(this,data);'而不是'B.m_algorithm'? – sergej
如果有任何問題,請參考'* this'。 – LogicStuff
對不起,我已將其更改 – Javier