在 c++ 中的多态方法上使用 std::for_each
当使用 std::for_each 时,
class A;
vector<A*> VectorOfAPointers;
std::for_each(VectorOfAPointers.begin(), VectorOfAPointers.end(), std::mem_fun(&A::foo));
如果我们有继承自 A 并实现 foo() 的类,并且我们持有一个指向 A 的指针向量, 有什么方法可以对 foo() 进行多态调用,而不是显式调用 A::foo() 吗? 注意:我不能使用boost,只能使用标准STL。
谢谢, 加尔
When using the std::for_each,
class A;
vector<A*> VectorOfAPointers;
std::for_each(VectorOfAPointers.begin(), VectorOfAPointers.end(), std::mem_fun(&A::foo));
If we have classes inheriting from A and implementing foo(), and we hold a vector of pointers to A,
is there any way to call a polymorphic call on foo(), rather then explicitly calling A::foo()?
Note: I can't use boost, only standard STL.
Thanks,
Gal
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它实际上是这样工作的。
打印
所以它完全符合你的要求。 不过,请检查
虚拟
关键字是否存在,但很容易忘记它们。It actually works this way.
prints
So it does exactly what you want. Check that
virtual
keywords are present though, it's easy to forget them.