Hi there everyone
If I need to pass a range of objects into some functions for processing, what would be the best way to specify which objects to process and which ones not to process??I could use a large bunch of if statements and say that If(what I want to filter==car){do car filtering} if(what I want to filter==bus){do bus filtering} However this soon becomes very dedious and not very efficicient. Is there an easy way to solve this? I know that you can use templates for different data types, but is it possible to do the same for different object names?
There are two approaches to this that I can think of. One if function overloading and the other involves giving your objects the ability to filter themselves:
Overloading functions
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
void do_filtering(bus& v)
{
// do bus filtering
}
void do_filtering(car& v)
{
// do car filtering
}
int main()
{
// do stuff
car c;
bus b;
do_filtering(c); // will call car filtering function
do_filtering(b); // will call bus filtering function
}
class vehicle
{
public:
virtualvoid filter() = 0;
}
class car
: public vehicle
{
public:
virtualvoid filter()
{
// do car filtering
}
}
class bus
: public vehicle
{
public:
virtualvoid filter()
{
// do bus filtering
}
}
int main()
{
// do stuff
car c;
bus b;
c.filter(); // will call car filtering function
b.filter(); // will call bus filtering function
}
ah yes, I see what you mean, The main problem I have is that I have about 7 different processing functions and not just one....I guess there possibibly isnt an easy method to solve it...