c++ 中的 std::future 是否支持多态性?
所以,如果要存储 child_class
在 future<parent_class>
,我可以在 dynamic_cast<child_class>
之后得到它吗? ?
最佳答案
如果您使用引用或指针(可能很明显,否则将无法编译)...是。
#include <iostream>
#include <future>
using namespace std;
struct Parent {
virtual void a() { cout << "I am parent"; }
};
struct Child : Parent {
virtual void a() { cout << "I am child"; }
};
Child g_c; //just some global for the purposes of the example
int main() {
std::future<Parent&> p = async(launch::async, []() -> Parent& { return g_c; });
auto c = dynamic_cast<Child&>(p.get());
c.a();
return 0;
}
代码结果在这里:http://ideone.com/4Qmjvc
关于c++ - std::future 是否支持多态?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40401331/