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
을 사용하는 더 좋은 방법이 있습니까? 그렇지 않다면 나는 아마도 람다를 고려할 것입니다.
죄송하지만 [라이브 예제] (http://coliru.stacked-crooked.com/a/86aa3493784bdb9a)는 광산 (VS2012) 용으로 컴파일 할 수 없습니다. 내가 놓친 게 있니? – cbel
@cbel : 저는 VS/Windows를 사용하지 않습니다. 죄송합니다. 정확한 오류 메시지는 무엇입니까? –
메시지는 이전과 동일합니다 ** 오류 C2064 : 용어는 2 개의 인수를 취하는 함수로 평가되지 않습니다 ** – cbel