What is the advantage of inherit from unary_function template

Discussion in 'C++' started by Allerdyce.John@gmail.com, Jan 26, 2006.

  1. Guest

    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;

    }
    , Jan 26, 2006
    #1
    1. Advertising

  2. Guest

    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.
    , Jan 26, 2006
    #2
    1. Advertising

  3. 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
    Victor Bazarov, Jan 26, 2006
    #3
  4. wrote:
    > 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
    Victor Bazarov, Jan 26, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. int
    Replies:
    7
    Views:
    349
    Cowboy
    May 20, 2004
  2. Larry Rekow

    Access advantage/liability

    Larry Rekow, Aug 24, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    351
    Larry Rekow
    Aug 24, 2004
  3. =?Utf-8?B?c3RldmVuIHNjYWlmZQ==?=

    can pages inherit from a template page

    =?Utf-8?B?c3RldmVuIHNjYWlmZQ==?=, Mar 3, 2006, in forum: ASP .Net
    Replies:
    2
    Views:
    349
    =?Utf-8?B?c3RldmVuIHNjYWlmZQ==?=
    Mar 6, 2006
  4. kyo
    Replies:
    0
    Views:
    317
  5. Replies:
    1
    Views:
    595
    Pete Becker
    Dec 1, 2007
Loading...

Share This Page