inherited static members

This is a discussion on inherited static members within the C++ Programming forums, part of the General Programming Boards category; I have a class, WIDGET. Code: class WIDGET{ public: WIDGET();//constructor ~WIDGET();//destructor int uid();//returns UID protected: WIDGET* parent;//parent widget pointer WIDGETINFO ...

  1. #1
    Registered User
    Join Date
    Jul 2006
    Posts
    19

    inherited static members

    I have a class, WIDGET.
    Code:
    class WIDGET{
        public:
            WIDGET();//constructor
            ~WIDGET();//destructor
            
            int uid();//returns UID
            
        protected:
            WIDGET* parent;//parent widget pointer
            WIDGETINFO info;//widget data info stuff, yeah...
            std::queue<EVENT> events;//Event queue
            
            CRONOS* cronosInstance;//cronos instance
            EVENTMANAGER* eventManInstance;//event manager instance
            WIDGETMANAGER* widgetManInstance;//widget manager instance
    
            static char* typeName;//widget type name
            
    };
    Code:
    char* WIDGETMANAGER::typeName = "WIDGET";
    And my derived BUTTON class.
    Code:
    class BUTTON:public WIDGET{
        public:
            BUTTON();
            ~BUTTON();
        private:
            
        protected:
    };
    Code:
    char* BUTTON::typeName = "BUTTON";
    However, as I'm sure you've already concluded this throws an error.
    ISO C++ does not permit `WIDGET::typeName' to be defined as `BUTTON::typeName'
    I guess I could stick this->typeName in the constructor for each derived class but I just thought keeping a static member would be easier and probably more efficient.
    Last edited by Smallz; 09-13-2006 at 04:58 AM.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >However, as I'm sure you've already concluded this throws an error.
    As it should. You seem to be confused about how static members work. typeName exists for all instances of the class in which the member is declared, including children. You don't need to redeclare and certainly not reinitialize typeName:
    Code:
    #include <iostream>
    
    class base {
    protected:
      static char *p;
    };
    
    char *base::p = "Base class static member";
    
    class derived: public base {
    public:
      derived()
      {
        std::cout<< p <<'\n';
      }
    };
    
    int main()
    {
      derived b;
    }
    On the other hand, if you need a static member for each child class, you need to make a new one and qualify to find the base member:
    Code:
    #include <iostream>
    
    class base {
    protected:
      static char *p;
    };
    
    char *base::p = "Base class static member";
    
    class derived: public base {
    public:
      derived()
      {
        std::cout<< base::p <<'\n';
        std::cout<< p <<'\n';
      }
    protected:
      static char *p;
    };
    
    char *derived::p = "Derived class static member";
    
    int main()
    {
      derived b;
    }
    But then I would question whether you really need the base class member to be protected when it could probably be private without any loss of functionality.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. seg fault at vectornew
    By tytelizgal in forum C Programming
    Replies: 2
    Last Post: 10-25-2008, 01:22 PM
  3. LNK2001 ERROR!!! need help
    By lifeafterdeath in forum C++ Programming
    Replies: 7
    Last Post: 05-27-2008, 05:05 PM
  4. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 04:42 PM
  5. Inherited static members
    By Vikstar in forum C++ Programming
    Replies: 2
    Last Post: 08-28-2003, 11:44 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21