Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > What is the advantage of inherit from unary_function template

Reply
Thread Tools

What is the advantage of inherit from unary_function template

 
 
Allerdyce.John@gmail.com
Guest
Posts: n/a
 
      01-26-2006
I would like to know what is the advantage of inherit from
unary_function template?

For example, I have this in my code:

What is the advantage of doing this:

class Same : std::unary_function<A*, bool>
{
public:
Same(A* a) : _a(a){};

bool operator()(A*a1);
private:
A* _a;
};


instead of this:

class Same
{
public:
Same(A* a) : _a(a){};

bool operator()(A*a1);
private:
A* _a;
};



typedef list<A*> AList
bool MyAList::contains(A* a) {
AList::iterator iter;

iter = find_if ( _aList.begin(), _aList.end(), Same(a));

if (iter == _aList.end())

return false;
else
return true;

}

 
Reply With Quote
 
 
 
 
roberts.noah@gmail.com
Guest
Posts: n/a
 
      01-26-2006

http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> I would like to know what is the advantage of inherit from
> unary_function template?
>
> For example, I have this in my code:
>
> What is the advantage of doing this:
>
> class Same : std::unary_function<A*, bool>
> {
> public:
> Same(A* a) : _a(a){};
>
> bool operator()(A*a1);
> private:
> A* _a;
> };


This allows you to use things like bind2nd and such. There are certain
requirements for "unary functions" and subclassing unary_function sets
all that up.

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      01-26-2006
(E-Mail Removed) wrote:
> I would like to know what is the advantage of inherit from
> unary_function template?
> [..]


At this point inheriting is done to get the typedefs 'unary_function'
declares (and 'binary_function' does that too).

V
 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      01-26-2006
(E-Mail Removed) wrote:
> (E-Mail Removed) wrote:
>
>>I would like to know what is the advantage of inherit from
>>unary_function template?
>>
>>For example, I have this in my code:
>>
>>What is the advantage of doing this:
>>
>>class Same : std::unary_function<A*, bool>
>>{
>> public:
>> Same(A* a) : _a(a){};
>>
>> bool operator()(A*a1);
>> private:
>> A* _a;
>>};

>
>
> This allows you to use things like bind2nd and such. There are certain
> requirements for "unary functions" and subclassing unary_function sets
> all that up.
>


Do you think you can show an example where _not_ inheriting would make
it impossible to use 'bind2nd'? Especially in the case of an _unary_
function. Thanks!

V
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
about unary_function LinLMa@gmail.com C++ 1 12-01-2007 01:24 PM
Microsoft SQL , Sybase advantage SQL,Advantage database Tommy Computer Support 1 11-29-2007 04:21 AM
can pages inherit from a template page =?Utf-8?B?c3RldmVuIHNjYWlmZQ==?= ASP .Net 2 03-06-2006 04:31 PM
Re: A Newbie Question about template template template Chris Theis C++ 2 07-24-2003 09:42 AM
Is there any difference? (about unary_function<>) kyo C++ 0 07-10-2003 08:06 AM



Advertisments