Archived

This topic is now archived and is closed to further replies.

maximAL

right use of flags?

Recommended Posts

i''m using a lot of boolean parameters so i thought about switching to flags. but i really can''t see a good way of using them, mainly because of global namespace cluttering. say i got a class Sprite with some function SetFlags(int Flags). now i got some flags like FLIP_H, FLIP_V, TRANSPARENT etc., all defined as const ints outside the class, what means nowhere else i could have a second TRANSPARENT flag. i could use some prefix (like SF_TRANSPARENT) or put the flags in a namespace (like SF::TRANPARENT -> really ugly for a dozen flags) but both doens''t look much better than just some plain parameters. would i gain any advantage by using flags here? maybe speed or sth.?

Share this post


Link to post
Share on other sites
Assuming you won''t pass those flags as parameter, you should gain a little speed improvement (but I doubt that you''ll even notice that ).

I usually #define things like FLIP_H, FLIP_V and so on (because these are things that do not need to be dynamically changed - but this shouldn''t make a difference to a const int, though)

But as for naming of these constants: I prefer the prefix thingie, because it looks neat and is quite handsome. No, seriously, I would do it this way, but that''s my own coding style and noone is obliged to stick to that (fortunately for you all *fg*).



Indeterminatus

--si tacuisses, philosophus mansisses--

Share this post


Link to post
Share on other sites
Typically I just define the flags as static constants in the class in question. I believe this is how the std::iosflags are handled. I have used this technique for several C++ and Java projects and it works well. The benifit is that the names can be short since no prefix is required as the constant is associated with a class and you can change the values without having to recompile a ton of code (sometimes). Another technique I have seen used is to have a class called Constants (or flags) with a ton of static consts so that the global namespace doesn''t become cluttered. Typically however these are universal constants (Avogadro''s Number, Boltzmann Constant, etc...) which are used in several classes. If I were you I''d simply put the flags as static constants in the Sprite class.

Dan

Share this post


Link to post
Share on other sites
quote:
Original post by vTheHero If I were you I'd simply put the flags as static constants in the Sprite class.


Sprite.SetFlags(Sprite::FLIP_H|Sprite::FLIP_V|Sprite::TRANSPARENT); doesn't look that good...
but ok, there seems to be no "perfect" way of doing this.


[edited by - maximAL on March 21, 2004 9:31:31 AM]

Share this post


Link to post
Share on other sites