Say, for example, I have classes CPrimus
, CSecundus
, and CTertius
that inherit from CParentis
, and I want to implement a tree of objects where the leaves (nodes?) can be instances of any one of the derived classes, e.g.:
- CPrimus
- CTertius
- CPrimus
- CSecundus
- CTertius
- CTertius
- CSecundus
- CSecundus
- CTertius
- CPrimus
- CTertius
- CSecundus
- CPrimus
- CTertius
What is the best* way to implement this tree in C++, and what modifications may be needed to the classes to support it?
(*) I will leave the definition of "best" up to the community, be it "fastest", "most efficient", "prettiest", or whatever you like. I personally have a preference for simple and efficient, but I'm willing to sacrifice simplicity for performance if the gain is worthwhile. Also, if you're going to suggest BGL please try to give an example, or at least link to an article or specific section of the documentation that explains how to implement the tree.
Edit:
Since the question is a bit too generalized, I'll give a potential use case. In Linux, a filesystem (commonly) will sit atop either a bare partition, an MD array, or an LVM logical volume. Assume I want my classes to represent these structures and store metadata about them (device name, size, uuid, etc etc) for later retrieval, and that the base class has a set of virtual functions for refreshing and retrieving the metadata.
Now, let's say that my system has a disk layout like so:
- [FS-/boot]
- [ARR-md0]
- [PART-sda1]
- [PART-sdb1]
- [ARR-md0]
- [FS-/]
- [LV-root]
- [VG-system]
- [ARR-md1]
- [PART-sda2]
- [PART-sdb2]
- [ARR-md1]
- [VG-system]
- [LV-root]
How would I represent this as a C++ data structure?