Type independent list

Started by
6 comments, last by u235 16 years, 2 months ago
In C# it was very easy to maintain a type independent list by making a list of type object which you could do pretty much anything to regardless of type because you could find the type of the object with an unboxing operation. Now I want to do the same thing in C++. I know how to determine the type of an object at run time via RTTI, but is it possible to use one of the STL container classes in a type independent manner? Thanks.
V/R,-AJThere are 10 kinds of people in the world: Those who understand binary and those who don't...
Advertisement
Are you looking for templates?
boost::any might help, but in general you should make your collections typed, even in C#. It'll make your programs more resilient since bad data will be prevented, and guide your design towards more reliable patterns.
a std::vector<void*> is probably the equivalent of a list of "objects" in C#
well, that's kinda what an STL container is for. It's a generic container than can contain a list of any type or polymorphic subtypes (via pointers).

Now, if you want to put a bunch of random jazz in a list, i would first question your reason for needing that. The same goes for RTTI. Sometimes you really do need that, but both of those things are often a symptom of poor design and indicate you are probably not using OOP properly.

People might be able to suggest alternatives if you post your specific issue.
In C#, all objects derive from common type, and the containers are equivalent of:
std::vector< boost::smart_ptr<Object> >;


In C++, you have the choice, you can either do the above, store the pointers to your objects, or use containers for same type of objects, which result in considerably lower memory footprint and better memory locality.

Languages have different idioms. What you want, while quite common, is almost never desirable in C++.

Containers and templates are compile-time polymorphism. This means that all types need to be fully defined at compile-time.

A better question here is, why are you using RTTI to discover the type of objects? Why not use polymorphism? Writing code that requires checking for data type is generally an indication of incorrect abstraction.

struct BaseFoo {  virtual void doFoo();};struct DerivedFoo {  virtual void doFoo();};std::vector< BaseFoo * > foos;foreach(...) {  foos->doFoo(); // don't care which type it is, no need to guess};
The feature you're tring to replicate is a really bad idea for the vast majority of scenarios - the only reason that containers of 'object' are pretty common in C# code is that C# 1.1 did not support generics.

Thank you for your suggestions and for pointing out what appears to be a significant flaw in my design. After re-evaluating my design, it appears that polymorphism would be the better choice. Thanks again and I'll try harder in the future to not base my design decisions for C++ based on what I could do in C# simply because it was easier in C#.
V/R,-AJThere are 10 kinds of people in the world: Those who understand binary and those who don't...

This topic is closed to new replies.

Advertisement