RPGDXThe center of Indie-RPG gaming
Not logged in. [log in] [register]
 
 
Post new topic Reply to topic Goto page Previous  1, 2 
View previous topic - View next topic  
Author Message
Rainer Deyke
Demon Hunter


Joined: 05 Jun 2002
Posts: 672

PostPosted: 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)

PostPosted: 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

PostPosted: 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

PostPosted: 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  
Post new topic Reply to topic Page 2 of 2 All times are GMT
Goto page Previous  1, 2 



Display posts from previous:   
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum