Skip to main content
gamedev.net
🔒 Locked

virtual static const

Started by cozman Apr 24, 2003 at 6:54 PM 3 replies 600+ views
Original Post
cozman
cozman
I''m aware you can''t declare data virtual but I''m looking for something to do something similar to "virtual static const" I''m writing a GUI class and everything is derived from a widget, and every listbox, button, window has a parent (NULL if parent is the global context) as well as a list of children. Classes like Buttons are not designed to have children, and I wanted a way to detect if somebody tries to create a widget using a non-container class as a parent.
  
class Widget
{
    private:
        Widget *mParent;
        WidgetList mChildren;
    public:
        Widget(Widget *parent=NULL);
       //and here''s what I wanted to add

        virtual static const bool CanParent;
};
  
then in the constructor
  
TextBox::TextBox(Widget *parent)
{
    if(parent && parent->CanParent)
        mParent = parent;
    else 
        mParent = NULL;
}
  
I have thought of one way to do this, but it seems hackish, and I was wondering if anybody had any cleaner suggestions for me.
chiuyan
chiuyan
either make it not static and assign its value in the appropriate constructor, or just make a CanParent funtion (that just returns false for buttons or whatever) and call that.
Sneftel
Sneftel
Your design is too simple by one class.

Have a Widget class. One class inherits from Widget: HierarchicalWidget. Widget does not have mChildren, but HierarchicalWidget does. And all widgets inherit from HierarchicalWidget or Widget, depending on whether they can have children.


How appropriate. You fight like a cow.
cozman
cozman
Thanks for the suggestions, I''ll play with both of those and see what works and what doesn''t, I was working on something similar to what you proposed Sneftel, yet the codependencies made me question that technique, but I believe I can get that working out, and in the end it''ll be the best solution.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.