Static members in derived classes

This is a discussion on Static members in derived classes within the C++ Programming forums, part of the General Programming Boards category; I'm a little stuck in another of Bruce Eckel's exercises. I'm given the following class "Framis" which sets aside a ...

  1. #1
    Registered User Sharke's Avatar
    Join Date
    Jun 2008
    Location
    NYC
    Posts
    303

    Static members in derived classes

    I'm a little stuck in another of Bruce Eckel's exercises. I'm given the following class "Framis" which sets aside a chunk of memory in the static data area for Framis objects, then uses an array of bytes to determine which blocks have been allocated. The new and delete operators are overloaded to utilize this allocation system:

    Code:
    class Framis {
      enum { sz = 10 };
      char c[sz]; // To take up space, not used
      static unsigned char pool[];
      static bool alloc_map[];
    public:
      enum { psize = 100 };  // frami allowed
      Framis() { out << "Framis()\n"; }
      ~Framis() { out << "~Framis() ... "; }
      void* operator new(size_t) throw(bad_alloc);
      void operator delete(void*);
    };
    unsigned char Framis::pool[psize * sizeof(Framis)];
    bool Framis::alloc_map[psize] = {false};
    
    // Size is ignored -- assume a Framis object
    void* 
    Framis::operator new(size_t) throw(bad_alloc) {
      for(int i = 0; i < psize; i++)
        if(!alloc_map[i]) {
          out << "using block " << i << " ... ";
          alloc_map[i] = true; // Mark it used
          return pool + (i * sizeof(Framis));
        }
      out << "out of memory" << endl;
      throw bad_alloc();
    }
    
    void Framis::operator delete(void* m) {
      if(!m) return; // Check for null pointer
      // Assume it was created in the pool
      // Calculate which block number it is:
      unsigned long block = (unsigned long)m
        - (unsigned long)pool;
      block /= sizeof(Framis);
      out << "freeing block " << block << endl;
      // Mark it free:
      alloc_map[block] = false;
    }
    I realize that this allocation system will not work for a class derived from Framis, because it relies on the size of Framis for calculations. So the question wants me to inherit from Framis and do what's needed to make the allocation work for the derived class. I figure I have to override the new and delete functions in order to use the correct size for the derived class, but I'm stumped as to what to do about the storage itself, i.e. pool[] and alloc_map[]. As I understand it I cannot override static data members in the derived class. But the initialization of pool[] uses size_of(Framis) and besides, I'm guessing that the derived class really needs its own storage area that's separate. So what can I do?

  2. #2
    Registered User
    Join Date
    Dec 2008
    Location
    Black River
    Posts
    128
    If you have to allocate the derived classes on the same pool as Framis', then you could round up the size to the nearest multiple of sizeof(Framis). For instance, let's say that sizeof(Framis) is 10, and that a derived class adds some members so that sizeof(Derived) is 16. You could use a function to compute the most appropiate multiple of sizeof(Framis), yielding 20, so you'd mark 2 blocks as used (You will be wasting some space in the process, though).

  3. #3
    Registered User Sharke's Avatar
    Join Date
    Jun 2008
    Location
    NYC
    Posts
    303
    Quote Originally Posted by Ronix View Post
    If you have to allocate the derived classes on the same pool as Framis', then you could round up the size to the nearest multiple of sizeof(Framis). For instance, let's say that sizeof(Framis) is 10, and that a derived class adds some members so that sizeof(Derived) is 16. You could use a function to compute the most appropiate multiple of sizeof(Framis), yielding 20, so you'd mark 2 blocks as used (You will be wasting some space in the process, though).
    That's certainly something I could do, however I can't quite see it being what Eckel had in mind. The question was simply this:

    Modify Framis.cpp from Chapter 13 by inheriting from Framis and creating new versions of new and delete for your derived class. Demonstrate that they work correctly.
    So I guess there's no constraint that says I have to use the same pool as Framis. In fact I would have thought that it would be better to have a separate pool for the derived class since there is nothing which suggests that Framis would never be instantiated. I suppose I could always create a differently named static pool for the subclass.

  4. #4
    Registered User
    Join Date
    Dec 2008
    Location
    Black River
    Posts
    128
    Quote Originally Posted by Sharke View Post
    So I guess there's no constraint that says I have to use the same pool as Framis. In fact I would have thought that it would be better to have a separate pool for the derived class since there is nothing which suggests that Framis would never be instantiated. I suppose I could always create a differently named static pool for the subclass.
    I suppose, though I guess that the exercise would be quite simple since you could use the example provided and replace Framis for whatever class you derived from it, if you're going to use different pools.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. initializing static members
    By steve1_rm in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2008, 04:45 AM
  2. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  3. deriving classes
    By l2u in forum C++ Programming
    Replies: 12
    Last Post: 01-15-2007, 04:01 PM
  4. static data members
    By ichijoji in forum C++ Programming
    Replies: 2
    Last Post: 07-05-2006, 05:18 PM
  5. Static Members...
    By BigDaddyDrew in forum C++ Programming
    Replies: 2
    Last Post: 02-11-2003, 07:46 PM

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