Passing two vectors into a function??

Jul 30, 2010 at 12:14pm
These are the two vectors that hold derived objects from the base classes Alclass and Axclass.

vector <Alclass> avector;
vector <Axclass> xvector;

If I want to pass either of these vectors from other functions to a single function (Myfunc) how is it accomplished? The internal logic of Myfunc only requires it to accept one vector at a time so that the objects in the vector can be worked on and attributes changed. To reiterate, both vectors are NOT required simultaneously in Myfunc.

I assume, Myfunc only needs one generic argument that accepts either vector when and if it is sent to that function. And it is that generic pass by reference of either vector that has me baffled.
Thanks for any help.
Jul 30, 2010 at 12:50pm
templates http://www.cplusplus.com/doc/tutorial/templates/
eg:
1
2
3
4
5
template < class T >
  void foo ( vector<T> &bar )
  {
     //...
  } 
Jul 30, 2010 at 1:01pm
Thanks. I've looked at templates but not understood what and how and where they can assist my problem.
Last edited on Jul 30, 2010 at 1:02pm
Jul 30, 2010 at 1:05pm
You make your Myfunc a template function, so it can be used for vector<Alclass> and vector<Axclass>
Otherwise just overload the function(write a vector<Alclass> version and write a second that takes vector<Axclass>), shouldnt be hard
Last edited on Jul 30, 2010 at 1:07pm
Jul 30, 2010 at 10:33pm
No worries. Thanks.
Topic archived. No new replies allowed.