K
Koen
Hi!
I have a vector containing the following:
vector<Base*> theVector;
theVector.push_back(new Der1());
theVector.push_back(new Der1());
theVector.push_back(new Der2());
theVector.push_back(new Der2());
....
where the Der1 and Der2 classes are derived from Base (abstract base
class).
Base
+--Der1
+--Der2
Each class derived from Base contains a "time stamp" and I have
implemented the operator: bool Base:perator<(const Base& inEvent)
const; that compares the time stamp of the object with that of the given
object.
Now, I would like to sort theVector according to the time stamp.
If I just call sort(theVector.begin(),theVector.end()); this doesn't
work because the "<" operator is never called (in fact, the addresses of
the objects in the list are compared directly).
Is there a way to have both things with STL containers:
1. store different object types (all derived from a single base class)
in a container (the only way to do this is to store pointers; otherwise
the objects get "sliced")
2. be able to use the standard algorithms on these containers
This must be possible, but I can't seem to find out how right now.
Any help appreciated!
Koen
I have a vector containing the following:
vector<Base*> theVector;
theVector.push_back(new Der1());
theVector.push_back(new Der1());
theVector.push_back(new Der2());
theVector.push_back(new Der2());
....
where the Der1 and Der2 classes are derived from Base (abstract base
class).
Base
+--Der1
+--Der2
Each class derived from Base contains a "time stamp" and I have
implemented the operator: bool Base:perator<(const Base& inEvent)
const; that compares the time stamp of the object with that of the given
object.
Now, I would like to sort theVector according to the time stamp.
If I just call sort(theVector.begin(),theVector.end()); this doesn't
work because the "<" operator is never called (in fact, the addresses of
the objects in the list are compared directly).
Is there a way to have both things with STL containers:
1. store different object types (all derived from a single base class)
in a container (the only way to do this is to store pointers; otherwise
the objects get "sliced")
2. be able to use the standard algorithms on these containers
This must be possible, but I can't seem to find out how right now.
Any help appreciated!
Koen