delorie.com/archives/browse.cgi | search |
From: | Patrick Peck <peck AT kfs DOT oeaw DOT ac DOT at> |
Newsgroups: | comp.lang.c++,comp.os.msdos.djgpp |
Subject: | Re: How to program a set of bits? |
Date: | Thu, 16 Oct 1997 19:20:48 +0200 |
Organization: | Academy of Science, Austria |
Lines: | 41 |
Message-ID: | <34464CEF.BB0DB2D3@kfs.oeaw.ac.at> |
References: | <34462916 DOT 7762 AT oce DOT nl> |
NNTP-Posting-Host: | mach.kfs.oeaw.ac.at |
Mime-Version: | 1.0 |
To: | djgpp AT delorie DOT com |
DJ-Gateway: | from newsgroup comp.os.msdos.djgpp |
A. Jans-Beken wrote: > In a program i'am writing I have a class like this: > > class PROPS { > unsigned long set_of_32_bits; > ... > } > > Now I want to have an operator '[]' so I can program: > > PROPS flags; > flags[2] = FALSE; > flags[4] = flags[31] = TRUE; > flags[6] = flags[3]; > > Please note that this is not the famous BITVECT example that can be > found in many books about c++. I do not want to use a pointer to a bit > set because the pointer takes up 4 bytes extra and i'am planning to use > a huge array of PROPS (and related classes). > > please respond via e-mail > thanx Hi, if you constrained yourself to a usage like bool b=flags[5]; you could easily define bool PROPS::operator[](int); If you absolutely need to have flags[5] on the left side of an assignment, things get more complicated; take a look at the std-library class 'bitset', where they use a helper class called 'reference' to do this; while notationally convenient, it sure lacks performance; Patrick
webmaster | delorie software privacy |
Copyright © 2019 by DJ Delorie | Updated Jul 2019 |