Sign in to follow this  
u235

Type independent list

Recommended Posts

u235    359
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.

Share this post


Link to post
Share on other sites
Telastyn    3777
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.

Share this post


Link to post
Share on other sites
leiavoia    960
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.

Share this post


Link to post
Share on other sites
Antheus    2409
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[i]->doFoo(); // don't care which type it is, no need to guess
};

Share this post


Link to post
Share on other sites
Nitage    1107
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.

Share this post


Link to post
Share on other sites
u235    359
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#.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this