Archived

This topic is now archived and is closed to further replies.

Passing a Polymorphed Parameter...

This topic is 5269 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Ok, I'm using polymorphism to abstract a templated class from the un-templated base class in order to create a template in template structure. This is so I can leave the sub-template objects open to determination until later, rather than defining them upon declaration of the primary structure, defeating the purpose of making them templated. So I have my (untemplated) GUI class, and my (templated) GUIT class. I have created a "GUILIST" Linked List, declared as a List of "GUI" objects, but add "GUIT" objects to the list through polymorphism so each GUILIST object can handle a different templated type. It looks like this:
GUILIST.Add(new GUIT(Parent, GUIwindow, &floatvar, 0, 100));  
My problem is with the "Parent" Parameter. I want each GUILIST element (should be GUIT objects) to have a member called "Parent" that points to another GUILIST element (a Parent GUIT object), and it should be passed in as a param in the constructor, like above. The problem is that the GUILIST items are *technically* (untemplated) 'GUI' objects, because that's what the GUILIST was declared as, but they are *really* (templated) 'GUIT' objects by polymorphism. I just can't seem to make the compiler happy with what I pass in, and was hoping to get some help. My example error was that I just tried passing in "GUILIST.GetLast()" (returns pointer to last list element) as the "Parent" parameter and get the error:
cannot convert parameter 1 from 'class GUI *' to 'class GUIT *' 
I can see why it would have the problem, but I can't figure out how to fix it. Thanks for any help

Share this post


Link to post
Share on other sites
I''m not sure what you mean... ?

I''ll take a stab at what you might be saying though. I tried switching it around by passing a ''GUI*'' object instead and changing the ''PARENT'' member into a GUI* but then I can''t work with the GUIT members I need to, and it got some wacky errors as well. If that''s not what you mean, could you explain a little?

Believe it or not, while working with more advanced concepts I''m suprised to find that I don''t know of some of the more basic ones. I''mnot really familiar with "ctor". Thanks for the response though

Share this post


Link to post
Share on other sites
quote:
Original post by neonfaktory
I just can''t seem to make the compiler happy with what I pass in, and was hoping to get some help.

Please post the code you are having trouble with.

Share this post


Link to post
Share on other sites
daerid is right. You should change GUIT* Parent in the constructor to GUI* Parent, then you can pass either a GUI* or a GUIT* to it. I''m suprised you didn''t do that seing as how you already know how polymorphism works.



-If you see this image I may or may not be online
My website

Share this post


Link to post
Share on other sites
Also, You could always cast GUILIST.GetLast() to a GUIT*. You would just do this:

GUIT* parent=dynamic_cast< GUIT* >(GUILIST.GetLast());
if(!parent)
{
// error, GUILIST.GetLast() is not a GUIT*
}
GUILIST.Add(parent,GUIwindow,&floatvar,0,100);

Also, if you use vc++ you have to enable runtime type identification (rtti) in project options.



-If you see this image I may or may not be online
My website


[edited by - brassfish89 on July 8, 2003 9:33:30 AM]

Share this post


Link to post
Share on other sites
Ahh, I see now daerid (and brass). I actually did try that, but got this error:
error C2440: ''='' : cannot convert from ''class GUI *'' to ''class GUIT *'' 

to my dismay.

Oh, and I have never heard of "dynamic_cast", but that gave me errors, too. Something about "...polymorphic object and may produce unexpected results", and then an error about not being able to convert.

I''m pretty stumped, but thanks for the help so far .

Share this post


Link to post
Share on other sites
Alright, I''ve got an idea.

The problems seems to be that polymorphism WOULD work in this situation, *if* the GUIT class wasn''t templated. At least thats what it seems like. Mainly because it wants to know what the template is upon passing it.

But I just realized that all I want to send is the Parent *Window*, which handles no variables - it''s just a window. Therefore, it doesn''t NEED to be templated. So I''m going to split up all the GUI types into different classes in my GUI.h file as opposed to being switch tested in GUIT, and my GUIwindow class will be un-templated. Hopefully it will work with it being untemplated. I''ll post back if it does for those who are curious.

Share this post


Link to post
Share on other sites