| View previous topic - View next topic | 
  
  
    | Author | Message | 
  
    | Rainer Deyke Demon Hunter
 
 
 Joined: 05 Jun 2002
 Posts: 672
 
 
 | 
        
          |  Posted: Thu Jun 19, 2003 1:39 am    Post subject: | [quote] |  
          | 
 |  
          | 'std::vector<bool>' is not a true 'std::vector'.  It is not even a true container.  It is a specialization that stores eight bools to a byte.  Because of this its 'reference_type' is not 'bool&'. 
 There are two ways of fixing your class, both with advantages and disadvantages.  Compare:
 
 
  	  | Code: |  	  | class EventList {
 vector<bool> events;
 
 vector<bool>::reference_type operator[] ( int index ) {
 return events [ index ];
 }
 };
 
 | 
 
 
  	  | Code: |  	  | class EventList {
 struct wrapped_bool {
 bool value;
 };
 vector<wrapped_bool> events;
 
 bool& operator[] ( int index ) {
 return events[index].value;
 }
 };
 
 | 
 
 The first implementation is more space-efficient and generally easier to write.  The second implementation gives you a true 'bool&' which makes it easier to use.
 |  | 
  
    | Back to top |  | 
  
    |  | 
  
    | PoV Milk Maid
 
  
 Joined: 09 Jun 2002
 Posts: 42
 Location: DrAGON MaX (Canada)
 
 | 
        
          |  Posted: Thu Jun 19, 2003 2:55 am    Post subject: | [quote] |  
          | 
 |  
          | Heh, or if you don't use Visual Studio (or even if you do), you should use "vector< bool >::reference" not "vector< bool >::reference_type".  Of course, if you don't care for portability, then by all means use reference_type, 'cause I doubt they'll depreciate it. 
 I shouldn't have to mention the operator should be public.. heh...
 |  | 
  
    | Back to top |  | 
  
    |  | 
  
    | grenideer Wandering Minstrel
 
  
 Joined: 28 May 2002
 Posts: 149
 
 
 | 
        
          |  Posted: Thu Jun 19, 2003 6:00 am    Post subject: | [quote] |  
          | 
 |  
          |  	  | Quote: |  	  | Inheriting doesn't give access to private data in the base only to protected.
 
 | 
 
 ack! did I say that?   :)
 _________________
 Diver Down
 |  | 
  
    | Back to top |  | 
  
    |  | 
  
    | Jihgfed Pumpkinhead Stephen Hawking
 
  
 Joined: 21 Jan 2003
 Posts: 259
 Location: Toronto, Canada
 
 | 
        
          |  Posted: Thu Jun 19, 2003 10:51 pm    Post subject: Thanks for vector<bool> Help | [quote] |  
          | 
 |  
          | Alright, thanks a lot for the help, everybody.  I think I've got a handle on this now.  Thanks again. |  | 
  
    | Back to top |  | 
  
    |  |