I'd want to expose some objects as abstract containers with stl-style manipulation possibilities (for_each loops, iterators) and hide container implementation details.
Performance issues don't matter (virtual calls and even memory allocation while copying "universal" iterator is acceptable).
I'm going to write an abstract container interface with pure virtual functions (+ "universal" iterator over the container) and an implementaition adapter for stl sequential containers.
But maybe there are useful existing libraries for this purpose?
Or it's totally a bad idea?
Thomas Becker has some useful advice (type erasure). You may also find Stroustrup's SCARY paper useful.
If your "objects" aren't STL objects but custom ones, I think it's a good idea.
As you can see on http://www.sgi.com/tech/stl/Vector.html , vector "is a model of" RandomAccessContainer. Most Boost packages use similar concepts (the term realy is "concept")
In C++, you've got two possibilities to do this :
With templates you can do something as :
doSomething < AnythingThatIsIterable >(AnythingThatIsIterable i){
for (AnythingThatIsIterable::itertaor it = i.begin(); it != i.end(); ++i){
it->foo()
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With