gpt4 book ai didi

c++ - C++ 的 STL 的 unary_function(在 gcc 4.7.x 中实现)可以有虚函数吗?

转载 作者:行者123 更新时间:2023-11-28 03:21:29 26 4
gpt4 key购买 nike

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 是一种方便的类型,可将几个 typedef 添加到您的派生类中。它不是多态的;如果您编写的代码在类定义中通过名称而不是作为基类提及它,那么您几乎肯定犯了错误。

关于c++ - C++ 的 STL 的 unary_function(在 gcc 4.7.x 中实现)可以有虚函数吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15295361/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com