M
Mark Turney
Problem:
I have a vector full of two different derived class objects (class B
and class C) that are derived from the same base class A. I want to
loop through vector and invoke a member function in only objects of
class B and skip over the objects of class C. To complicate things,
I'm using the vector position (index) as an argument in the invoked
member function. It is possible to move the position into the object
by adding a data member, but that feels like duplicating data when I'm
trying to keep things streamlined. I also think that needing to use
the vector index pretty much kills sorting or other use of iterators
or separate vectors.
The possible solutions that I have thought of are:
1. Calling a common virtual function of each object through a base
class pointer, while leaving the virtual function for class C (the
objects to skip) empty. Cons - Empty member functions seem ugly.
2. Using run-time type identification (typeid() == typid()) and a
series of if () else statments to skip the objects of class C. Cons -
The ensuing if-else tree feels like a botched switch statement.
3. Creating a static data member in each class that holds an int that
corresponds to the class type. Then, using this value in a switch
statement to skip over the objects of class C. Cons - I like the
switch in this step as opposed to solution 2, but it feels wrong
duplicating type information.
Am I missing something obvious, or if I'm moving in the right
direction, which of the solutions is the most elegant? If you're
feeling especially generous I would love to be told why any of my
ideas suck and why the best idea is the best.
Thanks for any help,
-Mark
I have a vector full of two different derived class objects (class B
and class C) that are derived from the same base class A. I want to
loop through vector and invoke a member function in only objects of
class B and skip over the objects of class C. To complicate things,
I'm using the vector position (index) as an argument in the invoked
member function. It is possible to move the position into the object
by adding a data member, but that feels like duplicating data when I'm
trying to keep things streamlined. I also think that needing to use
the vector index pretty much kills sorting or other use of iterators
or separate vectors.
The possible solutions that I have thought of are:
1. Calling a common virtual function of each object through a base
class pointer, while leaving the virtual function for class C (the
objects to skip) empty. Cons - Empty member functions seem ugly.
2. Using run-time type identification (typeid() == typid()) and a
series of if () else statments to skip the objects of class C. Cons -
The ensuing if-else tree feels like a botched switch statement.
3. Creating a static data member in each class that holds an int that
corresponds to the class type. Then, using this value in a switch
statement to skip over the objects of class C. Cons - I like the
switch in this step as opposed to solution 2, but it feels wrong
duplicating type information.
Am I missing something obvious, or if I'm moving in the right
direction, which of the solutions is the most elegant? If you're
feeling especially generous I would love to be told why any of my
ideas suck and why the best idea is the best.
Thanks for any help,
-Mark