class animal {
protected:
animal() {}
void eat(int x) {}
};
class human
: private animal
{
public:
typedef animal base_type;
using base_type::eat;
};
class stomach {
public:
stomach(std::function<void(int)> feed) {}
};
class lady
: public human
{
public:
typedef lady this_type;
typedef human base_type;
lady()
: base_type()
, m_Stomach(std::bind(&base_type::eat, this, std::placeholders::_1))
{
}
private:
stomach m_Stomach;
};
如果客戶端代碼寫下來:如何在這種情況下std :: bind基類的方法?
lady gaga;
編譯器抱怨std::bind(&base_type::eat, ...)
是錯誤C2064:術語不計算爲服用2個參數功能。
我已經發現,如果該類女士被修改爲:
class lady
: public human
{
public:
typedef lady this_type;
typedef human base_type;
lady()
: base_type()
, m_Stomach(std::bind(&this_type::help_eat, this, std::placeholders::_1))
{
}
private:
void help_eat(int x)
{
base_type::eat(x);
}
stomach m_Stomach;
};
有了一個幫助功能,編譯器會std::bind
好。但代碼重複。
如果改變std::bind
以拉姆達m_Stomach([&](int x){ base_type::eat(x); })
,這也可以被編譯我也發現了。
我的問題是沒有在這種情況下使用std::bind
更好的辦法?如果不是,我可能會考慮lambda。
很抱歉,但[實時示例](http://coliru.stacked-crooked.com/a/86aa3493784bdb9a)不能被編譯爲礦(VS2012)。我想念什麼? – cbel
@cbel:我不使用VS/Windows,對不起。什麼是你得到確切的錯誤信息? –
消息是一樣的,這是**錯誤C2064:術語不計算爲一個函數採取2個參數** – cbel