Template method implementation

Hi guys,
There's 2 classes:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream> 
#include <string>
#include <list> 

class Something{
private:
string word; 
int number;
public: 
string getWord() const; 
int getNumber() const;  
}

class Bigger{
list<Something> List;
public:
//A template function which will search in 'List' an object with matched 
// word/number(it will return an iterator to the object in 'List')
} 


I want to implement this function with template pattern in order to avoid code duplication. How do I do it? Thanks!
Last edited on
The usual approach would be to use the find_if algorithm
http://www.cplusplus.com/reference/algorithm/find_if/

To use it you have to code a function or functor (function object), which knows what a Something is, to act as the predicate.

Or did you want to code your own routine?

Andy
Last edited on
I want to write my own routine.
Someone please?
If you're unsure about templating, write it first first your specific case (using a list of Somethings)

Then, when it's working, convert it into the templated equivalent.

I assume you know how to iterate over a list??

Andy
Last edited on
Topic archived. No new replies allowed.