where to declare function object class

Discussion in 'C++' started by zs0723, Feb 28, 2008.

  1. zs0723

    zs0723 Guest

    I want to write my own function objects and used in vector<T>
    instanstance,
    for example

    class equalDate
    {
    string m_date;
    public:
    equalDate(string date):m_date(date){}

    bool operator()(string date)
    {
    return date == m_date;
    }
    };


    used in another class A's foo() function:
    void A::foo()
    {

    remove_if(vDate.begin(),vDate.end(),qualDate(baseDate));
    }

    my question is where to declare class "equalDate ", put it in class
    A scope
    or a separate file ?
     
    zs0723, Feb 28, 2008
    #1
    1. Advertising

  2. zs0723

    programmer Guest

    If you just use the class "equalDate" in class A, you can just
    declare it in the class A's header file. Otherwise, you need to
    declare it in a separate file.
     
    programmer, Feb 28, 2008
    #2
    1. Advertising

  3. zs0723

    Eric Pruneau Guest

    "zs0723" <> a écrit dans le message de news:
    ...
    > I want to write my own function objects and used in vector<T>
    > instanstance,
    > for example
    >
    > class equalDate
    > {
    > string m_date;
    > public:
    > equalDate(string date):m_date(date){}
    >
    > bool operator()(string date)
    > {
    > return date == m_date;
    > }
    > };
    >


    the class equalDate is useless in your case. The stl already has the
    template equal_to functor.
    and since string has an operator == you can just type

    remove_if(vDate.begin(),vDate.end(), bind2nd(equal_to<string>(), baseDate));
    // #include <functionnal> needed here

    by the way if you want to erase the elements equal to baseDate, you have to
    call erase with the returned iterator of remove_if.

    vector<string> NewEnd = remove_if(...);
    erase(NewEnd, vDate.end());


    Eric
     
    Eric Pruneau, Feb 28, 2008
    #3
    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. Tookelso
    Replies:
    3
    Views:
    608
    Steve Horsley
    Jun 30, 2004
  2. classicist
    Replies:
    3
    Views:
    347
  3. Replies:
    9
    Views:
    611
  4. Replies:
    1
    Views:
    1,399
    Jim Langston
    Nov 9, 2006
  5. Replies:
    2
    Views:
    695
    Triple-DES
    Feb 26, 2008
Loading...

Share This Page