范鸿飞 2004-6-14 11:50
为何我自己设计的函数对象不能用bind2nd绑定?
<!-- CETagParser ~code
<br><table cellpadding=0 cellspacing=0 border=0 WIDTH=94% bgcolor='#000066' align=center><tr><td><table width=100% cellpadding=5 cellspacing=1[/img]<TR><TD BGCOLOR='#f4f4f4'>template <typename elemType>
class morethan
{
public:
bool operator() (elemType val1, elemType val2)
{
return ( val1 >val2 );
}
};<!-- CETagParser ~/code
</td></tr></table></td></tr></table><br>
这是我自己设计的一个函数对象,比较前者是否大于后者。
我传给sort,一切正常,就像泛型库里的函数对象greater一样。
可当我用bind2nd绑定一个变量以传给find_if算法时,却被告知语法错误。
但同样的做法,如果换成标准STL库的greater,就一切正常。
这是为什么呢?
ilovesoup 2004-6-15 09:37
Re:为何我自己设计的函数对象不能用bind2nd绑定?
template <typename elemType>
class morethan:public binary_function<elemType, elemType, bool>
{
public:
bool operator() (elemType val1, elemType val2) const
{
return ( val1 >val2 );
}
};
ilovesoup 2004-6-15 09:38
Re:为何我自己设计的函数对象不能用bind2nd绑定?
1.greater使用了类型萃取,就在那个binary_function的继承上。
2.必须声明一个const函数
Kidoki 2004-6-15 09:42
Re:为何我自己设计的函数对象不能用bind2nd绑定?
[img]http://bbs.tongji.net/images/smiles/thumbs_up_smile.gif[/img] 灭错。
<functional>里,所有函数对象都是结构体binary_function和unary_function的继承。binary_function有first_argument_type,second_argument_type和result_type。
binder2nd是在其内部直接给second_argument_type赋值。
所以,楼主写自己的对象,要bind的话,最好继承binary_function或unary_function,其余就一样了。
我在路上 2004-7-26 00:45
Re:为何我自己设计的函数对象不能用bind2nd绑定?
想起了经典的设计模式原则:面向接口
8过C++的接口比java倒是弱多了。
OwnWaterloo 2008-10-8 02:57
Why! Why! Why!
[quote]原帖由 [i]我在路上[/i] 于 2004-7-26 00:45 发表 [url=http://bbs.tongji.net/redirect.php?goto=findpost&pid=3065087&ptid=158114][img]http://bbs.tongji.net/images/common/back.gif[/img][/url]
想起了经典的设计模式原则:面向接口
[/quote]
设计模式就是设计模式, 编程风格就是编程风格, 哪来的设计模式原则之说, 还面向接口 ……
风马牛不相及
[quote]原帖由 [i]我在路上[/i] 于 2004-7-26 00:45 发表 [url=http://bbs.tongji.net/redirect.php?goto=findpost&pid=3065087&ptid=158114][img]http://bbs.tongji.net/images/common/back.gif[/img][/url]
8过C++的接口比java倒是弱多了。
[/quote]
[b][color=red]Why! Why!! Why!!![/color][/b]
C++有ABC你知道不?
C++有多重继承你知道不?
Java 或者C#的接口, 是为了限制多重继承的能力, 但又舍不得完全丢掉这个特性而搞出来的一个东西,你知道还是不知道?
你没写反吧?!!!
OwnWaterloo 2008-10-8 03:09
[quote]原帖由 [i]范鸿飞[/i] 于 2004-6-14 11:50 发表 [url=http://bbs.tongji.net/redirect.php?goto=findpost&pid=2791188&ptid=158114][img]http://bbs.tongji.net/images/common/back.gif[/img][/url]
这是为什么呢?
[/quote]
因为你设计的函数对象不满足[color=red][b]可适配[/b]函数对象[/color]的[b][color=red]concept[/color][/b]s。
[code]yourFunctor::result_type;
yourFunctor::first_argument_type;
yourFunctor::second_argument_type; // for AdaptableBinaryFunction
result_type yourFunctor:: operator() ( const first_argument_type& f ) /* const is not necessary */ ;
result_type yourFunctor:: operator() ( const first_argument_type& f ,const second_argument_type& s) /* const is not necessary */ ;
参数列表也不一定非要 ( const first_argument_type& );
本质特征是: 必须满足如下的调用语法。
functor( a1 , a2 ); 就OK
[/code]
[quote]原帖由 [i]ilovesoup[/i] 于 2004-6-15 09:38 发表 [url=http://bbs.tongji.net/redirect.php?goto=findpost&pid=2796646&ptid=158114][img]http://bbs.tongji.net/images/common/back.gif[/img][/url]
1.greater使用了类型萃取,就在那个binary_function的继承上。
2.必须声明一个const函数 [/quote]
不一定非要const函数。
函数对象[color=red]按值传递[/color]。
可以有函数对象在被调用时修改内部状态。
[quote]原帖由 [i]Kidoki[/i] 于 2004-6-15 09:42 发表 [url=http://bbs.tongji.net/redirect.php?goto=findpost&pid=2796664&ptid=158114][img]http://bbs.tongji.net/images/common/back.gif[/img][/url]
<functional>里,所有函数对象都是结构体binary_function和unary_function的继承。binary_function有first_argument_type,second_argument_type ... [/quote]
[color=red]不一定[/color]要从unary_function 或 binary_function 继承。
由它们继承[color=red]仅仅[/color]是为了能[color=red]方便[/color]得到1或者2个typedef [b][color=red]而已[/color][/b]。
如果必要, 完全可以自己typedef。
必要的情况, 如:
编译器的[color=red]空基类优化[color=black]([/color]Empty Base Class [color=#333333][color=red]Optimize[/color])[/color][/color]能力不足。
类似的还有std::iterator 也是一个有争议的设计。
[[i] 本帖最后由 OwnWaterloo 于 2008-10-8 03:15 编辑 [/i]]