This isn't very good for the following reasons:
1) Your class is mixing layers of abstraction by having a getIterator function in the first place. Your public member functions should speak in the problem domain your class is meant to solve, "getIterator" is about as useful as a function named "getInt".
2) You are breaking encapsulation by forcing the consumers of your class to understand the exact iterator types of (I'm assuming) private data... This is just conceptually wrong.
3) Even assuming this was a good idea, the function itself is taking a uint8 which has a huge possible range, it should really take a limited type or a named enum, are you using #define to specify the two types available? Don't do that...
4) But it isn't a good idea, this shouldn't be a templated function, it should be broken up into two appropriately named functions because the function is doing two different things! Write functions which do one thing at one layer of abstraction.
5) If you're passing back a begin iterator how will your class consumers know the end of the range!? You'd need to provide an equivalent end iterator and at this point you may as well just make the vectors public...
6) But you shouldn't do that either, instead consider whatever range based behavior you need to implement as either an in-class function which does the iteration and transformation based on passed in parameters, or alternatively supply a callback method which transforms each item similar to std::for_each or other algorithm functions. I don't know what your class is doing, but returning an iterator is a bad idea, returning the contents of the vector is fine.
7) Your function is trying to return two different types... Each version of the template would have an error (if you try to make a template of one type it would have the possibility of returning the other if you supply the wrong value to the parameter which of course is not possible as it results in a compile time error.) You can instantiate a template which returns a specified type provided the return value is consistent, but not if the generated template functions mix and match return types as you are doing here.
IE:
vector<boo*>::iterator it = pc.getIterator<vector<boo*>::iterator>(TYPE_BOO);
Results in a template function like this:
vector<boo*>::iterator getIterator(uint8 type){ vector<boo*>::iterator result; switch(type) { case TYPE_FOO: { result = m_xFoos.begin(); } break; case TYPE_BOO: { result = m_xBoos.begin(); } break; } return result;}
In this case let's just assume m_xFoos.begin() returns vector<foo*>::iterator, you would get a conversion error when this template function is generated and attempts to compile.
So, not only is this technically wrong, it is also conceptually bad. In a weak typed language which allowed stuff like this to happen it would still be wrong.
Props, however, for the appropriately named "poorClass". :) I kind of laughed a bit at the name. Hopefully this advice isn't too gruff for you! It's just to the point, I think it's great that you've asked it here in the beginner forum and hopefully the answer is helpful.
FINALLY, here is a different implementation that will do what you expect it to based on simple operator overloading called from a template, do not use it, but enjoy!
template <typename T>T getIterator(){ T returnIterator; setIterator(returnIterator); return returnIterator;}private:void setIterator(vector<boo*>::iterator &i){ i = m_xBoos.begin();}void setIterator(vector<foo*>::iterator &i){ i = m_xFoos.begin();}
You could also simply cut out the template and use the setIterator functions directly, but instead of returning an iterator you'd have to make one first and pass it in.
I'm not near a compiler right now, so I apologize for any problems with it. Calling syntax would be:
vector<boo*>::iterator it = pc.getIterator<vector<boo*>::iterator>();
If you made the setIterator methods public you could just do this:
vector<boo*>::iterator it;
pc.setIterator(it);
Of course you'd want to re-name it because setIterator sounds like you're applying the value of it to something instead of actually returning it.
[Edited by - M2tM on October 21, 2010 8:11:45 PM]
_______________________"You're using a screwdriver to nail some glue to a ming vase. " -ToohrVyk