comparison operators parameter

P

Philipp Kraus

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
 
Ö

Öö Tiib

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

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top