C++ hierarchy as tree

This is a discussion on C++ hierarchy as tree within the C++ Programming forums, part of the General Programming Boards category; Hi Say that I have : Code: #include <iostream> class base{}; class derived1 :public base{}; class derived2 : public base{}; ...

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    20

    C++ hierarchy as tree

    Hi

    Say that I have :
    Code:
    #include <iostream>
    
    class base{};
    class derived1 :public base{};
    class derived2 : public base{};
    class double_derived : public derived1{};
    
    int main()
    {//code
      return 0;
    }
    Can I in some way have manipulate that hierarchy as tree. More specifically without knowing the children derived1 , derived2 and double_derived ( I know only the root node base ) to parse the tree and create objects for each class. A nice output would be to show me the full hierarchy ( base is the root , derived1 inherit from base , derived2 inherit from base, double_derived inherit from derived1 ) Then while I know the children ( class ) to be able to create objects .

    In deeper my problems is
    1)How can I pass from the root to children ( RTTI provide the class that belongs an object and the before() function compares already known objects and classes ) WHAT I NEED is to find the class and subclasses dynamically through parsing tree
    2)If in some way I find the names of classes , how can I dynamically create objects ? This is a problem because objects declared at compilation time
    3)Is there any way C++ to answer in that question : What is the objects of class X and what are the subclasses of class X

    Thanks
    Last edited by Salem; 07-26-2004 at 02:17 AM. Reason: Added code tags - please find out how to use them before posting more code

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    1) You have to create special data structures for this - on your own. As you said, RTTI is not extensive enough. For this, you need to parse the source files yourself. The Qt tools do something like this.
    2) Extend the structures from 1) to contain addresses of either constructors or creator functions (and don't ask me how to find them...). This requires you to adapt the structures to the ABI your compiler is using and greatly restricts portability.
    3) Only with above structures.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    To answer the second question, I am rather fond of using a factory class. Basically, what you do if give each of your classes in the hierarchy a static construction function with the same signature (returning a pointer to the base class, and taking the same parameters), and give your factory a static registration function that takes a string (the name of the class, for example, or an integer identifier, or whatever), and a pointer to a function of the same signature as the constructor function. Then, when you want to create one, you call the factory's get function and pass it a string. It will then look into its registry, and use the appropriate construction function.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Binary Tree Search
    By C++Newbie in forum C++ Programming
    Replies: 7
    Last Post: 04-05-2011, 01:17 AM
  2. Interpreter.c
    By moussa in forum C Programming
    Replies: 4
    Last Post: 05-28-2008, 05:59 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  5. BST/Red and Black Tree
    By ghettoman in forum C++ Programming
    Replies: 0
    Last Post: 10-24-2001, 10:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21