comparison operators parameter

Discussion in 'C++' started by Philipp Kraus, May 21, 2011.

  1. Hello,

    I' writing a class with a method, that should get a parameter for
    comparision like <, <=, > or >=. The method must compare some float
    values but the user of the method should be set the comparision. My
    first idea is to use an enum with 4 states of comparision, but is there
    a better solution?

    In pseudocode:

    myclass::mymethod( comparision_operator p_comp ) {
    float a,b;
    if ( p_comp(a,b))
    do something
    }

    Thx

    Phil
    Philipp Kraus, May 21, 2011
    #1
    1. Advertising

  2. Philipp Kraus

    Öö Tiib Guest

    On May 21, 8:58 pm, Philipp Kraus <> wrote:
    > Hello,
    >
    > I' writing a class with a method, that should get a parameter for
    > comparision like <, <=, > or >=. The method must compare some float
    > values but the user of the method should be set the comparision. My
    > first idea is to use an enum with 4 states of comparision, but is there
    > a better solution?
    >
    > In pseudocode:
    >
    > myclass::mymethod( comparision_operator p_comp ) {
    >     float a,b;
    >     if ( p_comp(a,b))
    >        do something
    >
    > }


    Usually function objects are used there in C++. Objects of class that
    overloads 'bool operator()(float,float) const'.
    Öö Tiib, May 21, 2011
    #2
    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. J Jones

    Comparison Operators

    J Jones, Dec 23, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    333
    J Jones
    Dec 24, 2003
  2. Michael Klatt
    Replies:
    4
    Views:
    450
    Michael Klatt
    Oct 21, 2003
  3. Replies:
    1
    Views:
    578
    Mike Treseler
    May 15, 2006
  4. Replies:
    2
    Views:
    357
    Ferdi Smit
    Oct 23, 2005
  5. Joe Van Dyk

    Comparison operators

    Joe Van Dyk, Aug 10, 2006, in forum: C++
    Replies:
    4
    Views:
    291
    Joe Van Dyk
    Aug 10, 2006
Loading...

Share This Page