Different object names

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?

Cheers

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
}


Or make your objects able to filter themselves:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
class vehicle
{
public:
	virtual void filter() = 0;
}

class car
: public vehicle
{
public:
	virtual void filter()
	{
		// do car filtering
	}
}

class bus
: public vehicle
{
public:
	virtual void 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...
At the end of the day you can not avoid having to do seven different things. All you can do is organise it in a way that is easiest to understand.
Topic archived. No new replies allowed.