Thread: Inherited static members

  1. #1
    Registered User
    Join Date
    Jul 2003
    Posts
    4

    Inherited static members

    I'm trying to get a static instance of an inherited class per base class. I.e, given the following code:
    Code:
    #include <stdio.h>
    
    class A {
    public:
      static int newIndex() {int i = index++; return i;}
    private:
      static int index;
    };
    
    int A::index = 0;
    
    class B : public A {
    public:
     B() {}
    };
    
    class C : public A {
    public:
     C() {}
    };
    
    int main(int argc, char **argv) {
      B *b = new B();
      C *c = new C();
      int bi = B::newIndex();
      int ci = C::newIndex();
      printf("bi = %i, ci = %i\n", bi, ci);
      return 0;
    }
    This code is compilable, and when runs it produces:
    bi = 0, ci = 1

    What I would like it to produce is:
    bi = 0, ci = 0

    That is, I would like to have a unique static instance of the "index" variable for every class that inherits A, instead of one global index variable which is the case at the moment. Is there anyway I can do this so that developers inheriting class A will get the desired functionality implictly (i.e. without using overkill software engineering techniques such as making the developer construct singleton classes and add them to each derived class instance)?

    Thanks in advance,
    Lukasz.

  2. #2
    Registered User
    Join Date
    Aug 2003
    Posts
    127
    look at this code
    I don't know what your meaning is,so the "index" is a counter,not a index,

    Code:
    #include <stdio.h>
    template<typename T>
    class A {
      static int index;
    public: 
      static int newIndex() {return index++;}
    
    };
    
    template<typename T> int A<T>::index = 0;
    
    class B : public A<B> {
    public:
     B() {}
     static int newIndex(){ return A<B>::newIndex(); }
    };
    
    class C : public A<C> {
    public:
     C() {}
     static int newIndex(){ return A<C>::newIndex(); }
    };
    
    int main(int argc, char **argv) {
      B *b = new B();
      C *c = new C();
      int bi = A<B>::newIndex();
      int ci = A<C>::newIndex();
      printf("bi = %i, ci = %i\n", bi, ci);
      delete b;  //Do not miss this expression
      delete c;
      return 0;
    }
    Last edited by jinhao; 08-28-2003 at 04:47 AM.
    Nana C++ Library is a GUI framework that designed to be C++ style, cross-platform and easy-to-use.

  3. #3
    Registered User
    Join Date
    Jul 2003
    Posts
    4
    Thanks. This is almost exactly what I was after. However, I wanted to achieve this without the need for any work from the developer appart from simply inheriting the base class A. Although having to override the newIndex() function isn't a tall order, I was hoping that there was some way to do it without such a mundane task that has to be done the same way for every derived class of A.

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, 05:42 PM
  5. Using private class members in static functions
    By sethjackson in forum C++ Programming
    Replies: 2
    Last Post: 09-23-2005, 09:54 AM