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. Advertisements

  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. Advertisements

  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. Advertisements

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:
    778
    Steve Horsley
    Jun 30, 2004
  2. E11
    Replies:
    1
    Views:
    5,755
    Thomas Weidenfeller
    Oct 12, 2005
  3. classicist
    Replies:
    3
    Views:
    425
  4. Replies:
    9
    Views:
    814
  5. freegnu
    Replies:
    2
    Views:
    472
    freegnu
    Oct 23, 2006
  6. Replies:
    1
    Views:
    1,688
    Jim Langston
    Nov 9, 2006
  7. =?gb2312?B?wfXquw==?=
    Replies:
    10
    Views:
    924
    Victor Bazarov
    Aug 1, 2007
  8. Replies:
    2
    Views:
    841
    Triple-DES
    Feb 26, 2008
Loading...