2 Layer Template Structures Useless?

Started by
22 comments, last by neonfaktory 20 years, 10 months ago
So I have my GUI design figured out. I use my standard Linked List class that I''ve been using for the rest of my program to hold my master list of GUI elements. It is obviously templated. However, my GUI class is also templated. Like I''m sure others have done, all the buttons and textboxes are "windows" but with different aspects to them. 1 scroll bar on the screen might be altering an "int" while another might be altering a "float". The natural solution is to make the GUI class templated. But there''s the problem. Upon declaration of the master gui list "GUILIST" that keeps track of all the elements, it requires you to define what type of template the actual GUI elements in that list will be... defeating the purpose of having them templated if they all have to be the same type anyway. Soo, am I doing this wrong? Can I have a templated Linked List class that holds differently templated GUI elements? Thanks for any help
weee!
Advertisement
don''t make the GUI class templated. Just derrive from a base class and store pointers.
... That''s pretty general. "store pointers"? Pointers to what? The values the GUI elements are changing? If so, that is why the class is templated because those values could be the "int"s I store ammo with or the "float"s I use to store weapon-specific percentages with.

Besides, thats not the main point of the post. Can you have a templated class use another templated class as a template without "fixing" the enclosed template to a specific datatype? Thanks again for any help
weee!
Yep, a GUI system is actually a perfect candidate for a polymorphic inheritance tree. Ie. you have a CDialog base class, with standard control and event management functionality (mostly implemented by virtual functions). From that base class, you derive your individual controls:

class CDialog { /* base class functionality */ }

class CButton : public CDialog { /* ... */ }

class CDropdown : public CListbox, CButton { /* ... */ }

class CSpecialDropdown : public CDropdown { /* ... */ }

etc...

About your approach: well, I don't knwo your details, but it sounds like you're abusing the template feature. If you wanted a float/int Scrollbar, then simply template the derived type:

template <class T> CScrollbar : public CWhatever { /* ... */ }


[edited by - Yann L on May 7, 2003 6:03:20 PM]
quote:Original post by neonfaktory
Besides, thats not the main point of the post. Can you have a templated class use another templated class as a template without "fixing" the enclosed template to a specific datatype? Thanks again for any help


Short answer? No.
daerid@gmail.com
To Yann:
What you mentioned is exactly what I'm doing, "template CScrollbar : public CWhatever" more or less. The problem arises when I want to create a Linked List of them. The Linked List class itself is templated, and when I try to make a list of the templated "CScrollbar" class with the already templated "LinkList" class, it asks to know what template the "CScrollbar" class will be upon declaration, instead of leaving it open to determine later, defeating the whole point of making the CScrollbar class templated...

Is there any way to work around this?

[edited by - neonfaktory on May 7, 2003 12:04:21 AM]
weee!
Hold the parent type?
Your linked list should be a list of pointers to your common base GUI class. The fact that the derived classes (such as scrollbars etc) are templated makes no difference.



[ MSVC Fixes | STL Docs | SDL | Game AI | Sockets | C++ Faq Lite | Boost
Asking Questions | Organising code files | My stuff | Tiny XML | STLPort]
quote:Can you have a templated class use another templated class as a template without "fixing" the enclosed template to a specific datatype?
Yes, of course.
  template<typename T>class SomeClass {};template<template<typename> class K>class SomeOtherClass {public:    typedef K<int> whatever;};int main(){    typedef SomeOtherClass<SomeClass>::whatever whatever;}  
This should be mentioned in any good C++ book.

- lmov
- lmov
Well, maybe this will clear it up some, because I didn't think what I was doing was that complicated...

          template<class itemType>class CList    // My Linked List Class - Works Fine...template<class itemType>class GUI      // My Gernerac GUI Class...// Then I simply want to be able to say:CList<GUI> GUILIST; // <--- Doesn't work, says "GUI" needs a class type// So I can do:GUILIST.Add(new GUI(...));    // And pass ints/floats/etc. to the GUI constructor// ^^^ All this does not work.// The line that declares "GUILIST" gives an error about "GUI" needing a class type, as in...CList<GUI<type>> GUILIST;     // <--- Doesn't work either (bad syntax)  


Whatever the syntax is, it defeats the purpose of me being able to add GUIs with different templated types to the GUILIST... Gah!

*EDIT* Code Examples needed source tags

[edited by - neonfaktory on May 8, 2003 11:00:42 AM]
weee!

This topic is closed to new replies.

Advertisement