Overloading Array Operators[]

This is a discussion on Overloading Array Operators[] within the C++ Programming forums, part of the General Programming Boards category; I want to provide [] for my bitmap, but it is stored as an array of uint32_t's, therfore a reference ...

  1. #1
    Password:
    Join Date
    Dec 2009
    Location
    NC
    Posts
    587

    Overloading Array Operators[]

    I want to provide [] for my bitmap, but it is stored as an array of uint32_t's, therfore a reference to a bool is out of the question. Is there a solution, or am I stuck with the ol' get/set interface?

    Just for the heck of it:
    Code:
    #include <stddef.h>
    #include <stdint.h>
    
    template <size_t bits, bool default_value = false>
    class bitmap
    {
        public:
        bitmap()
        {
            blocks =  bits / 32 + (bits % 32 == 0 ? 0 : 1);
            reset_all();
        }
        
        bool get(size_t index)
        {
            if(index >= bits)
                return default_value;
                
            return data[index / 32] & ~(1 << index % 32) != 0;
        }
        
        void reset(size_t index)
        {
            set(index, default_value);
        }
        
        void reset_all()
        {
            set_all(default_value);
        }
        
        void set(size_t index, bool value)
        {
            if(index >= bits)
                return;
            
            if(value)
                data[index / 32] |= 1 << index % 32;
            else
                data[index / 32] &= ~(1 << index % 32);
        }
        
        void set_all(bool value)
        {
            for(size_t i = 0;i < blocks;i++)
                data[i] = value ? ~(uint32_t)0 : (uint32_t)0;
        }
        
        private:
        size_t blocks;
        uint32_t data[bits / 32 + (bits % 32 == 0 ? 0 : 1)];
    };

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,667
    Why would it be out of the question? You're using bool as a proxy type to represent a single bit. You would have to use a proxy class anyway.

  3. #3
    Password:
    Join Date
    Dec 2009
    Location
    NC
    Posts
    587
    Code:
    bool &operator[] (size_t index)
    {
         return data[index / 32] & ~(1 << index % 32) != 0;
    }
    How do I get referenced bool stored back into my bitmap, if say, the user changes it?
    Last edited by User Name:; 01-27-2011 at 12:30 AM.

  4. #4
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,667
    All you have to do is wrap said bool in a proxy class, with position information, and return a reference to that instead.

    Since bits[x] = something; involves two operators, you can easily define what happens in those.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Overloading subscript operator for multi-dimensional array.
    By minesweeper in forum C++ Programming
    Replies: 3
    Last Post: 05-12-2011, 10:57 PM
  2. Replies: 7
    Last Post: 11-25-2008, 12:50 AM
  3. array of pointers/pointer arithmetic
    By tlpog in forum C Programming
    Replies: 18
    Last Post: 11-09-2008, 06:14 PM
  4. 1-D array
    By jack999 in forum C++ Programming
    Replies: 24
    Last Post: 05-12-2006, 07:01 PM
  5. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM

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