STL List and Templates

Started by
2 comments, last by fathom88 15 years, 12 months ago
I'm trying create a stl list of heterogenous elements. Normally, one would use the list as such. std::vector<bool> BunchOfBools; std::vector<int> BunchOfInts; std::vector<float> BunchOfFloats; I would like to do something like the following. std::vector<MySpecialAnyKind> BunchOfAnything; To maximize expandibility, I defined my MySpecialAnyKind class as a template. Yes, I can do MySpecialAnyKind<int> or MySpecialAnyKind<float>. Then, I'm back to square one. I thought about 1st defining an empty base class. Then, deriving MySpecialAnyKind from MyBaseClase, which really does nothing and is empty. class MyBaseClase { void MyBaseClase(); ~MyBaseClase(); }; template <class Special> class MySpecialAnyKind : MyBaseClase { public: Special DoSomething(); //return template specific }; Then, I would do this. std::vector<MyBaseClase *> BunchOfAnything; I could do an insert with the following new MySpecialAnyKind<int>(); Here is my problem. How do I get the correct MySpecialAnyKind pointer from the list? Should I check the pointer type? I tried to do a dynamic cast but my compiler (VC++ 7.0) didn't like it one bit. Any suggestions? Thanks in advance.
Advertisement
What about boost.any?
What are you going to use this for?
To be honest, it's purely acedemic at this point. I alreay found a way around the problem. I just made a vector<int>, vector<string>, vector<bool> you get the idea. My original problem was I used to create a structures and made linked lists all the time to do calculations in my proggrams. I came from C and wanted to learn how to use the advanced features of C++. I created a templated linked list class. Cool! No more re-writing the same linked list code over and over. My structures would have muliple types like int's and string's(mostly for ID purposes). Now, I basically creating a Variant class and using the stl's built in functionality. It's more about learning at this point.

This topic is closed to new replies.

Advertisement