Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jul 26th, 2004, 3:08 AM   #1
cfriend
Newbie
 
Join Date: Jul 2004
Posts: 8
Rep Power: 0 cfriend is on a distinguished road
Hi

Say that I have :

#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
cfriend is offline   Reply With Quote
Old Jul 26th, 2004, 5:47 PM   #2
Infinite Recursion
Programming Guru
 
Infinite Recursion's Avatar
 
Join Date: Jul 2004
Location: United States
Posts: 3,467
Rep Power: 8 Infinite Recursion is on a distinguished road
Send a message via MSN to Infinite Recursion Send a message via Yahoo to Infinite Recursion
I think you can do this using the Standard Templates Library. I haven't personally tried it, but give SGI's STL a whirl at:

http://www.sgi.com/tech/stl
__________________
http://jasonpowers.net

"There are a thousand hacking at the branches of evil to one who is striking at the root."
Infinite Recursion is offline   Reply With Quote
Old Jul 27th, 2004, 7:27 PM   #3
kurifu
Expert Programmer
 
kurifu's Avatar
 
Join Date: Jul 2004
Location: Halifax, Nova Scotia (Canada)
Posts: 784
Rep Power: 5 kurifu is on a distinguished road
Send a message via ICQ to kurifu Send a message via MSN to kurifu
Look up explicit and implicit casting (particularly the first) and look into the static_cast<>(), const_cast<>(), and dynamic_cast<>() operators, the will allow you to morph your objects to whatever type you want so that you can use all the type generically.

I am not sure what you are talking about though when you say manipulate them as a tree? If you are trying to create some sort of tree in the memory you do not have to used derived classes, you can also look into using unions.

static_cast<> and const_cast<> are nearly the same, one is type safe the other is not (as I recall), dynamic_cast<> is also type safe, and it will actually make use of your class' vtable and change the class so that it behaves exactly like the class you are morphing to.
__________________
Clifford Matthew Roche &lt;geek@cliffordroche.com&gt;
Web Hosting: http://www.crd-hosting.com
Consulting: http://www.crdev-consulting.com
kurifu is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 8:10 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC