c++ STL的unary_function(在gcc 4.7.x中实现)可以有虚函数吗?
Can unary_function of STL of C++ (as implemented in gcc 4.7.x) have a virtual function?
binder2nd
来源于unary_function
下面的代码片段不起作用:
greater<int> g;
//greater is derived from binary_function
//bind2nd returns binder2nd object and binder2nd is derived from unary_function
const unary_function<int,bool> &greater_than_0 = bind2nd(g, 0);
// base class reference type holding reference to derived class object
remove_if(lst.begin(), lst.end(), greater_than_0);
//does not work
而下面的代码片段可以工作:
greater<int> g;
const binder2nd< greater<int> > &greater_than_0 = bind2nd(g, 0);
remove_if(lst.begin(), lst.end(), greater_than_0);
在unary_function
中有一个虚拟函数是否有帮助,以便第一个代码片段工作。这能改善STL的使用吗?
unary_function
是一种方便的类型,它为派生类添加了两个类型。它是非多态;如果您编写的代码在类定义中通过名称而不是作为基类提到它,那么几乎可以肯定您犯了一个错误。
相关文章:
- 在命名空间内部还是外部实现 c++ 函数?
- 如何在C++中实现函数(f)(x)(y, z)(g)(r)
- 如何在C++中实现函数上的二叉搜索?
- 键按下事件错误 Qt 实现函数时
- 在子类之外实现函数导致未知错误
- 通过模板滥用实现函数式C++
- C++,实现函数"int next(std::string param)"时出现奇怪的编译器错误
- 我正在尝试创建自己的 strcat() 函数,而不是使用库实现函数<cstring>
- 如果未实现函数,则在链接时启用错误
- 用函数参数实现c++函数指针
- 如何在Android SDK中调用和实现C++函数
- 在 C++ RCPP 中实现 R 函数
- 实现函数模板填充多维对象
- 类的层次结构,试图在基本级别实现函数
- 用c++实现函数对象的自动检测类型
- 用shared_ptrs实现函数模板的C++实例化
- 在类中实现函数导致错误:成员引用基类型'ifstream (string)'不是结构或联合
- 用c++实现S函数.生成错误
- 引用可以用于实现函数重写吗
- 如何在c++中实现函数超时