Using 'flags'

This is a discussion on Using 'flags' within the C++ Programming forums, part of the General Programming Boards category; hey guys, I'm starting to create a class that will require a series of structure values to be set and ...

  1. #1
    uh oh
    Join Date
    Jan 2005
    Location
    Ontario, CA
    Posts
    66

    Using 'flags'

    hey guys, I'm starting to create a class that will require a series of structure values to be set and then that structure passed to the class' constructor during creation. one of these will be a series of flags that can be set to a DWORD flag type variable.

    its rather simple to set the flags, e.g.

    Code:
    #define CLS_FLAGONE 0x34000000     // just random values
    #define CLS_FLAGTWO 0x00FF0000     // obviously not the best choices ;)
    #define CLS_FLAGTHREE 0x0000A100
    .
    .
    .
    egStruct.flags=CLS_FLAGONE|CLS_FLAGTHREE; // DWORD variable
    .
    .
    Now please let me know if I've even done that part wrong. However, my question is... how do you get those values out of flags, or test for their presence in the flags variable? I have heard that MFC provides easily accessible methods for this, but I don't use MFC. Any ideas?

    Thanks.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    If you set flags with |, then you test with &. For instance:
    Code:
    #define FLAGONE 0x01
    #define FLAGTWO 0x02
    #define FLAGTHREE 0x04
    #define FLAGFOUR 0x08
    
    .
    .
    .
    
    int flags = FLAGONE | FLAGTHREE;
    
    .
    .
    .
    
    if (flags & FLAGONE) printf("Flag one set\n");

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,900
    If the struct is rather short, then I don't recommend passing a struct at all. Instead just pass all arguments via a constructor.
    Another option is to just construct the object, then call various member functions to initialize the object. Much easier.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    uh oh
    Join Date
    Jan 2005
    Location
    Ontario, CA
    Posts
    66
    tabstop, thanks. I guess I skipped things while teaching myself C... probably wont entirely help during my phase in to C++ lol.

    Elysia, I have a lot of experience with the C language (with gaps apparently *sigh*). I'm a week in to C++ programming and after looking the term object up in my big C++ book that I am just opening now, if I got it right, you're meaning:

    Code:
    class Example {
    public:
          Example(DWORD someFlags, char *someName/*, possibly other params */);
          ~Example();
    
          void ChangeName(char *newName);
          void RemoveFlag(DWORD remFlag);
          void AddFlag(DWORD addFlag);
          char* displayFlags();
    
    private:
          DWORD flags;
          char *name;
          /* some other "settings" here */
    };
    
    int main(void) {
          Example eg=new Example(EG_ALLFLAGS, "MyTitle");
          eg.RemoveFlag(EG_FLAGONE | EG_FLAGTWO);
          cout<<eg.displayFlags();
    }
    And, yes, I omitted or was careless in some spots on purpose just to simply make a small example to see if I got the concept right. I am assuming this is what you meant by objects? If so, I am unaware of any other method of accessing a class without doing so. If I am wrong, thouh, can you provide me with any direct reference material you might have handy? I would like to fill in the gaps rather then continue on with an erronous-belief in what I know.

    Much appreciated, both of you.
    Last edited by cyreon; 01-28-2008 at 12:26 PM. Reason: Sample wasn't doing as I would like it show.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,900
    Code:
    class Example
    {
    public:
    	Example(Param1, Param2, Param3, Param4, etc...);
    	//...
    };
    A constructor approach. You can overload as many constructors as you want, in case you want to fill in a lot information or just a little.
    Or you could use another idea:

    Code:
    class Example
    {
    public:
    	void SetMyFlag1(...);
    	void SetMyFlag2(...);
    	void SetMyFlag3(...);
    	void SetMyFlag4(...);
    	void SetMyFlag5(...);
    	void SetMyFlag6(...);
    };
    So in that example, you could expose member functions so you can manipulate the state of an object to your need, if you just need to modify some of the flags.

    Or you can do a little of both.
    Oh, and an object is what is referred to as an instantiated instance of a class. A class is an object in terms that it is self contained and you should control it (like you do a car), but not make the object work internally or run (the object or the car would do all that work for you). Hence, they're referred to as objects.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    uh oh
    Join Date
    Jan 2005
    Location
    Ontario, CA
    Posts
    66
    Elysia, I actually have fallen in love with using classes and so I've pretty much already taken a very similar approach to what you are suggesting. Though you do bring up some interesting ways outside of my own, thanks once again for the informative response.

    Now to go finish this project.

  7. #7
    uh oh
    Join Date
    Jan 2005
    Location
    Ontario, CA
    Posts
    66
    Elysia, you seem to know what your doing (and btw I like the quote in your signature :P). Do you happen to know of any website that has all the standard typedef's, variable types, sizes (in bits), etc. type info? Also, by typedef I'm meaning say DWORD. Right now I'm resolving to seeking out the appropriate .h's and finding it on my own.

  8. #8
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,118
    Do you happen to know of any website that has all the standard typedef's, variable types, sizes (in bits), etc. type info?
    It depends on just what exactly do you mean by standard (e.g., DWORD is not a standard C++ typedef, but it is standard on say, Windows). You can buy a copy of the C++ Standard for a few dozen US dollars or find a draft copy online, but then you will find that the sizes of the built-in types are left implementation defined, with only things like the minimum size and relative sizes specified.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,900
    No, unfortunately, I don't.
    Unfortunately, as well, these typedefs can vary on platform and compiler and as can the size.
    The types I know: char, unsigned char, whcar_t, unsigned short, short, unsigned int, int, long, unsigned long, long long, __int64, unsigned long long, unsigned __int64.
    More typedefs: IN8, UINT8, INT16, UINT16, INT32, UINT32, INT64, UINT64.
    Char = 1 byte, wchar_t = 2 bytes (on Windows), short = 2 bytes, int/long = variable, can be 2/4/8 bytes I believe, long long = 4 or 8 bytes, __int64 = 8 bytes.
    DWORD = unsigned long, 4 bytes typically.
    There are also typedefs for upper-case names: CHAR, SHORT, INT, LONG.

    Remember that many are compiler and platform specific.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    The entire point of typedefs like "DWORD" is that they can be set to match whatever compiler is being used. (DWORD, by the way, comes from the original Intel x86 assembler language, and means "double word", where a single "word" is 16 bits).

    Since the number of bits (in _nearly all_ architectures) is 8 bits, you can figure out the number of bits in a given type (this example shows DWORD) with
    Code:
    int bits = sizeof(DWORD) * 8;
    I believe ALL Windows platforms use 4-byte DWORD's, but there are a few other types that change size depending on processor architecture - there is a type called "LONG_PTR" in Windows.h that is "the size of a pointer".

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  12. #12
    uh oh
    Join Date
    Jan 2005
    Location
    Ontario, CA
    Posts
    66
    Wow, thats a bit of information in a very short period. Thanks all, appreciate all of that.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. obtaining which combination of flags are being set
    By stanlvw in forum C++ Programming
    Replies: 4
    Last Post: 07-16-2008, 03:43 AM
  2. Cannot get interface flags: Invalid argument
    By nasim751 in forum C Programming
    Replies: 1
    Last Post: 04-15-2008, 03:27 AM
  3. Cannot get interface flags: Invalid argument
    By nasim751 in forum C Programming
    Replies: 3
    Last Post: 04-14-2008, 03:29 AM
  4. Bit Flags
    By Padawan in forum C Programming
    Replies: 15
    Last Post: 03-30-2004, 10:38 PM
  5. Flags, the ambiguity of
    By Jeremy G in forum C++ Programming
    Replies: 1
    Last Post: 01-25-2003, 11:41 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21