473,434 Members | 1,818 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,434 software developers and data experts.

static const in struct

dj
Perhaps this question should be in the standard c newsgroup, but i hope
somebody answers it here. Anyway, I came across this situation in an
otherwise c++ code.

I need a struct that works like some sort of a flag set. The flag mask
is a single long, while the meaning of individual flags is given by
static const members of the same struct:

struct flag_set {
long flags;
static const long FLAG1_BIT = 0x1;
static const long FLAG2_BIT = 0x2;
...
static const long FLAGN_BIT = 1 << N-1;
}

Of course the consts have meaningful names in my code, so i can check
for some condition like this:

flag_set fs;
....
if (fs & flag_set::FLAGX_BIT) {
do something;
}

I suppose this is one of many possible ways to approach the problem but
i am not sure if it is the best one as well. The storage should not be
the problem, even with many flag_sets floating around, because static
members don't use additional space (i am pretty sure about that).
However, i have not seen much code written this way and would like to
know if someone knows for a better way to do it, that is for some reason
why the upper solution is not good.

If nothing else, this is a question of proper programming style that i
still need to work on. All answers without "this is a stupid question"
in them are much appreciated.
May 15 '06 #1
3 6591
dj wrote:
Perhaps this question should be in the standard c newsgroup, but i hope
somebody answers it here. Anyway, I came across this situation in an
otherwise c++ code.

I need a struct that works like some sort of a flag set. The flag mask
is a single long, while the meaning of individual flags is given by
static const members of the same struct:

struct flag_set {
long flags;
static const long FLAG1_BIT = 0x1;
static const long FLAG2_BIT = 0x2;
...
static const long FLAGN_BIT = 1 << N-1;
}

Of course the consts have meaningful names in my code, so i can check
for some condition like this:

flag_set fs;
...
if (fs & flag_set::FLAGX_BIT) {
do something;
}

I suppose this is one of many possible ways to approach the problem but
i am not sure if it is the best one as well. The storage should not be
the problem, even with many flag_sets floating around, because static
members don't use additional space (i am pretty sure about that).
However, i have not seen much code written this way and would like to
know if someone knows for a better way to do it, that is for some reason
why the upper solution is not good.

If nothing else, this is a question of proper programming style that i
still need to work on. All answers without "this is a stupid question"
in them are much appreciated.


There's nothing innately wrong with the code. I would tend to prefer
using an enum within the class rather than static constants just
because it is less typing. You might also consider abstracting the bit
twiddling by hiding the enum and providing inline member functions like
this:

class FlagSet
{
private:
long flags_;

enum
{
FLAG_1 = 1,
FLAG_2 = 2,
// ...
};
public:
bool IsFlag1Set() const { return flags_ & FLAG_1; }

FlagSet& SetFlag1()
{
flags_ |= FLAG_1;
return *this;
}

FlagSet& ClearFlag1()
{
flags_ &= ~FLAG_1;
return *this;
}

// ...
};

void Foo( FlagSet& fs )
{
if( fs.IsFlag1Set() )
{
fs.SetFlag2().SetFlag5().ClearFlag7();
}
}

The setting and clearing business in the if-statement's body uses
method chaining (cf.
http://www.parashift.com/c++-faq-lit...tml#faq-10.18).

Cheers! --M

May 15 '06 #2
>> suppose this is one of many possible ways to approach the problem but
i am not sure if it is the best one as well. The storage should not be
the problem, even with many flag_sets floating around, because static
members don't use additional space (i am pretty sure about that).
However, i have not seen much code written this way and would like to
know if someone knows for a better way to do it, that is for some reason
why the upper solution is not good.


How about an enum?

struct flag_set {
long flags;
enum FLAG {
FLAG1 = 1,
FLAG2 = 1 << 1,
FLAG4 = 1 << 2,
...,
FLAGN = 1 << sizeof(long)-1
};
};

The usage would be the same:

flag_set fs;
fs.flags = flag_set::FLAG1 | flag_set::FLAG2 | flag_set::FLAG4;

if (if (fs.flags & flag_set::FLAG4) {
// do something
}

May 15 '06 #3
I can't see anything wrong with your approach or with apporach which
uses enums for flag mask values. These are valid approaches, which
don't take use of already made class that handles flags operations.
Thus, my suggestion would be to use STL bitset class.

#include <bitset>
#include <iostream>

void main()
{
std::bitset<20> b;

b.set(5); // turn on bit number 5

if (b.test(5))
{
// do something
}

std::cout << b[5] << std::endl;
}

I showed only small use of it. There is much more to it. For instance,
you could flip bits in certain places using "flip" method, check if
there's any of the flags turned on with "any" method,
ie: if (b.any()) {...} and much more.

May 15 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Jon Slaughter | last post by:
Is there any way to declare a member of a base class to make the itself in the derived class to be static? i.e., I have two classes class Base { protected: char *name;
13
by: Alex Vinokur | last post by:
I have the following problem. class Base { public: void (*pfunc) (int) = 0; }; class Derived : public Base {
1
by: Piotr Sawuk | last post by:
just a quick question out of curiosity: how to initialize const arrays? struct srat { long num; ulong den; srat(){} } struct collisions
15
by: rwf_20 | last post by:
I just wanted to throw this up here in case anyone smarter than me has a suggestion/workaround: Problem: I have a classic producer/consumer system which accepts 'commands' from a socket and...
5
by: partha.p.das | last post by:
Here is my class: class MyClass { public: MyClass(); ~MyClass(); private:
3
by: salunkerahul | last post by:
Hello All, I have some static libraries generated on MSVC 2005 express edition and I have to use them along with static libraries created with MSVC 2003 and create an application on MSVC2003. ...
23
by: anon.asdf | last post by:
Hello! In the following code-snippet, is it possible to initialize each element of arr, with STRUCT_INIT? struct mystruct { int a; char b; };
6
by: mathieu | last post by:
Hi there I am trying to provide a lookup from two 'int's into a char array, something like this: template <int g, int estruct Lookup; template <struct Lookup<0,0{ // some typedef + enums...
11
by: Jef Driesen | last post by:
I have the following problem in a C project (but that also needs to compile with a C++ compiler). I'm using a virtual function table, that looks like this in the header file: typedef struct...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.