[英]Virtual member functions and std::tr1::function: How does this work?
[英]How to store a virtual member function in std::function?
class foo
{
public:
foo(void)
{
this->f = std::bind(&foo::doSomething, this);
}
private:
virtual void doSomething(void) { }
private:
std::function<void(void)> f;
}
class bar : public foo
{
public:
bar(void) { /* I have no idea what I have to */ }
private:
virtual void doSomething(void) override { }
}
我想將覆蓋的'doSomething'函數分配給'foo :: f'。 但我不知道如何分配重寫的“ doSomething”功能。 還是我只寫一些代碼為每個類分配“ doSomething”功能?
class foo
{
public:
foo(void)
{
this->f = std::bind(&foo::doSomething, this);
}
private:
virtual void doSomething(void) { }
private:
std::function<void(void)> f;
}
class bar : public foo
{
public:
bar(void)
{
this->f = std::bind(&bar::doSomething, this);
}
private:
virtual void doSomething(void) override { }
}
該代碼是我對問題的回答。 但是我想我可以將虛擬函數自動分配給std :: function。
this->f = std::bind(&foo::doSomething, this);
這樣很好。 通過指針或引用傳遞對象將允許它調用正確的虛函數。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.