Class's and typecasting
Hiya,
for my GUI, I am making one basic template class that all my control class''s will be derived from. The basic class has features for say coloring, texture mapping, stuff like that. The class''s that will be derived from that one are say bitmap buttons, labels, radio buttons, stuff like that. I am working on doing the saving and loading of my GUI so that I can make a simple editor for it. I was wondering on ways to do this. What I was thinking is that I write the file with a data-type number representing the different controls. Say 1 is a bitmap button, 2 is a label, etc. When I read those back in, is there a way to type cast a variable of the base class to the specific component.?
Here''s and example
GUI_Base temp;
// read number
if ( number == 1 ) // bitmap button
{
temp = (GUI_Button)temp;
}
My question is would that work and are there better ways to do that. Thanks a lot.
Bye
Shawn
Surely you can just do this:
GUI_Object * obj;
GUI_Button * btn; // derived from GUI_Object
// read generic control properties from the file
switch (number)
{
case 1:
btn = obj;
// now read button specific properties from the file
break;
case 2:
...
}
Is this what you kinda wanted?
GUI_Object * obj;
GUI_Button * btn; // derived from GUI_Object
// read generic control properties from the file
switch (number)
{
case 1:
btn = obj;
// now read button specific properties from the file
break;
case 2:
...
}
Is this what you kinda wanted?
July 11, 2000 09:56 AM
Take a look at a resource file generated with VC++. That should give you some good ideas.
You can always cast from base to derived with dynamic_cast
Dynamic_cast returns zero if your cast is invalid.
Edited by - Armitage on July 11, 2000 11:22:54 AM
Edited by - Armitage on July 11, 2000 11:23:33 AM
//You have Gui_derived1, and Gui_derived2Gui_base *bptr = new Gui_derived1();//Further downGui_derived1 *d1ptr;Gui_derived2 *d2ptr;if (d1ptr = dynamic_cast<Gui_derived1 *>(bptr))...else if (d2ptr = dynamic_cast<Gui_derived2 *>(bptr))...
Dynamic_cast returns zero if your cast is invalid.
Edited by - Armitage on July 11, 2000 11:22:54 AM
Edited by - Armitage on July 11, 2000 11:23:33 AM
A polar bear is a rectangular bear after a coordinate transform.
July 11, 2000 11:37 AM
TPH ,
Correct me if I am wrong but
btn = obj;
doesn''t look right to me, I think that you should explicitly detail the cast you want to do ( assigning a pointer from the base class to the derived one without casting will generate a compiler error, though casting from the derived class to the base class won''t)
btn = static_cast obj;
We can use the dynamic_cast as mentionned by armitage but only if the base class or any of its derived classes have at least one virtual function within.
Not being an expert in C++ I may be wrong so excuse me if that''s the case
Correct me if I am wrong but
btn = obj;
doesn''t look right to me, I think that you should explicitly detail the cast you want to do ( assigning a pointer from the base class to the derived one without casting will generate a compiler error, though casting from the derived class to the base class won''t)
btn = static_cast obj;
We can use the dynamic_cast as mentionned by armitage but only if the base class or any of its derived classes have at least one virtual function within.
Not being an expert in C++ I may be wrong so excuse me if that''s the case
July 11, 2000 12:04 PM
In the code above you should read
btn = static_cast (obj);
for some reason the GUI_Button* thingy doesn''t appear in the previous post I wrote
btn = static_cast (obj);
for some reason the GUI_Button* thingy doesn''t appear in the previous post I wrote
July 11, 2000 12:08 PM
Damnit , the code i write isn''t diplayed correctly so the static casting instruction you can read is not correct, it should look like armitage dynamic_cast instruction safe that you replace dynamic_cast with static_cast.
Don''t forget to put the RTTI on if you are using dynamic_cast and check that your class is polymorphic ( which is most of the time the case)
Don''t forget to put the RTTI on if you are using dynamic_cast and check that your class is polymorphic ( which is most of the time the case)
Whoops! You are absolutely right. Sorry, didn''t mean to mislead you. I''ve done a experiment in VC5 and it seems to work with both static_cast and reinterpret_cast.
My knowledge of C++ doesn''t extend to understanding the differences between these though!
My knowledge of C++ doesn''t extend to understanding the differences between these though!
July 12, 2000 01:07 PM
Here is my answer :
never use reinterpret_cast if you don''t have to.
That casting operator is very dangerous, you are supposed to use it only if the object you are casting doens''t have any obvious link to the object that will received the cast. Using it shows poor coding (no offense meant ), you''d better fall back to static_cast , dynamic_cast (if it''s a polymorphic class).
Yu Ominae
never use reinterpret_cast if you don''t have to.
That casting operator is very dangerous, you are supposed to use it only if the object you are casting doens''t have any obvious link to the object that will received the cast. Using it shows poor coding (no offense meant ), you''d better fall back to static_cast , dynamic_cast (if it''s a polymorphic class).
Yu Ominae
Hi,
There is no need to cast anything, that would create a total mess.
Define derived classes (e.g. Button, Label, etc) from your (abstract) base class using virtual functions as needed.
As you read in from the file ''new'' the associated derived class. Since you will have lots of these objects add it to a collection. Then adjust the parameters as needed from the file. Repeat as needed.
zx495
There is no need to cast anything, that would create a total mess.
Define derived classes (e.g. Button, Label, etc) from your (abstract) base class using virtual functions as needed.
As you read in from the file ''new'' the associated derived class. Since you will have lots of these objects add it to a collection. Then adjust the parameters as needed from the file. Repeat as needed.
zx495
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement