Thread: Set of binary properties. Vector of bool?

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    115

    Set of binary properties. Vector of bool?

    Hi,
    I have a very basic question. What is the appropriate data type for what essentially is a collection of bools, taking the value true if a certain property exists? For example, assume there are 9 properties in total,
    Code:
    {Red, Yellow, Green, Thick, Thin, Young, Old, Bad, Good}
    any of which can be possessed by a class instance,
    Code:
    MyClass Example({Red, Green, Young})
    Functions (own and general) are then modified depending on whether a certain property is set.
    I was thinking of using an enum, but verifying whether a property exists in an enum not straight forward. In principle, vector of bools would do. Even bits of an integer. But then I don't see the labels, just the indices, which impairs readability.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    It sounds like you want a struct with nine bool member variables.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    May 2008
    Posts
    115
    Quote Originally Posted by laserlight View Post
    It sounds like you want a struct with nine bool member variables.
    The downside is I would need to pass all booleans to the constructor (assuming default values of false and a default constructor), even if only few of them need to be set, resulting in a long initialisation command, or set the values individualy by calling each field. There would be no consistency in code between cases with few and those with many true values.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Then use bitwise operators to set named bits that you then inspect so as to populate the struct of bools. Or instead of bitwise operations, you can use function chaining to set the members of the struct of bools directly in a consistent manner.

    EDIT:
    Actually, if you are going to use bitwise operations, it probably doesn't make sense to translate it into a struct of bools. You might as well go with an unsigned int or std::bitset. std::vector<bool> probably isn't that good a fit either since you have a fixed number of flags. The key to overcoming "I don't see the labels, just the indices" is to combine these bitwise operations with an enum of the properties.

    If you do go with a struct of bools, then the function chaining thing should fit the bill quite well, e.g.,
    Code:
    struct Flags
    {
        bool red = false;
        bool green = false;
        bool thin = false;
        bool young = false;
    
        Flags& Red()
        {
            red = true;
            return *this;
        }
    
        Flags& Green()
        {
            green = true;
            return *this;
        }
    
        Flags& Thin()
        {
            thin = true;
            return *this;
        }
    
        Flags& Young()
        {
            young = true;
            return *this;
        }
    };
    
    // ...
    
    MyClass Example(Flags().Red().Green().Young());
    Last edited by laserlight; 09-26-2019 at 06:06 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    May 2008
    Posts
    115
    This looks good, thank you! But how what should be in MyClass (I guess all possible properties), and what should be in its constructor?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    MyClass just needs to process the Flags object in its constructor.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    May 2008
    Posts
    115
    But is it possible to nest a struct like this in another struct and pass its argument to the parent struct constructor?

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Why don't you give it a try? I don't see the point of nesting though.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. bool vector operations - a more efficient way needed
    By baxy in forum C++ Programming
    Replies: 7
    Last Post: 11-08-2015, 03:50 AM
  2. bool typedef and binary compatibility
    By BattlePanic in forum C Programming
    Replies: 2
    Last Post: 05-08-2008, 08:04 AM
  3. How get bool's from std::vector<bool>
    By 6tr6tr in forum C++ Programming
    Replies: 6
    Last Post: 04-14-2008, 05:24 AM
  4. vector<bool> push_back
    By ygfperson in forum C++ Programming
    Replies: 4
    Last Post: 03-05-2003, 08:48 PM

Tags for this Thread