How to extract derived classes from the base-class container?

After storing objects of different types in the same container using common parent class I need to extract them back.


int main()
    element wrapper; = "div";
    wrapper.attributes["id"] = "wrapper";

    cargo<string> text("Learn from yesterday, live for today, hope for tomorrow.");


    cout << "Name:\t"  << << endl;

         /* I have an explicit cast here,
          * but it can't be used this way
          * since children may have different types
    cout << "Cargo:\t" << ((cargo< string >*) wrapper.children[0])->value << endl;

    return 0;


struct element
    std::string name;
    std::map< std::string, std::string > attributes;
    std::vector< node* > children;


struct node
{ };


template <typename Type>
struct cargo
    : public node
    Type value;

    cargo(Type value)
        : value(value)
    { }

I need to have some kind of type holder to be associated with real node type and use it in farther casting-extracting operations... Instead of that hard-coded one in my test.


What I'm trying to do is a simple Document Object Model Data structure to use it as symbol table entry for my xml-like language parser. I don't want to use any existing XML library as they are very large. I think the idea of DOM is simple, so I can easily adopt it for some more complex operations, for example, by allowing generic types for the nodes in DOM tree using cargo<Type>. I recognize that the design I adopted may not be the most adequate! So I'm open to suggestions!

I would be thankful for any help!


This question is probably more about the design than implementation. Although Boost.Variant and Boost.Any will work, they will be only a workaround. The real problem may be that variable part of responsibility of classes, derived from node class, is not encapsulated. You could try to use composition instead. One host class used for common interface and appropriate amount of components/delegates/whatever (those are to be born from a solution design :) ).

Or... a totally different solution may fit you. You may want to venture to meta programing word and ditch the common interface. Instead entities like tuples (type lists) may be of help.

Best Regards, Marcin

if you are simply streaming, you could implement the stream operators in the base class and then delegate to a method in the derived class, else look at the visitor pattern. Without having a real grasp of what kind of operations you are likely to be doing on cargo, it's difficult to make further suggestions...

If you don't plan on treating the container members polymorphically on retrieval, Boost.Variant might be useful to wrap the container members in a deterministic way.

The variant class template is a safe, generic, stack-based discriminated union container, offering a simple solution for manipulating an object from a heterogeneous set of types in a uniform manner. Whereas standard containers such as std::vector may be thought of as "multi-value, single type," variant is "multi-type, single value."

There's some example code in this prior question.

Need Your Help

Best way to sandbox Apache on Linux

linux security apache debian sysadmin

I have Apache running on a public-facing Debian server, and am a bit worried about the security of the installation. This is a machine that hosts several free-time hobby projects, so none of us who...

Notification manager is not working in count down timer class in android

java android notifications

I am using a countdown timer class and in the onfinish() state of that class I am creating a notification. Here is the Onfinish method's code: