Thread: Pass bitset pattern to constructor

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

    Pass bitset pattern to constructor

    Hello,

    How can I pass an expression similar to the one I commented out (instead of the binary representation) to the constructor? I would like to make the constructor argument more readable.

    Code:
    #include <iostream>
    #include <bitset>
    
    struct Person
    {
    	const char* Name;
    	int Age;
    	std::bitset<4> State{};
    	Person(const char* name, int age, std::bitset<4> state) : Name(name), Age(age), State(state) {}
    };
    
    int main()
    {
    	std::bitset<4> isBad{ 0b0001 };
    	std::bitset<4> isSad{ 0b0010 };
    	std::bitset<4> isMad{ 0b0100 };
    	std::bitset<4> isFat{ 0b1000 };
    
    	Person me("Joe", 25, std::bitset<4>{ 0b0101 });
    	//me.State |= isBad | isMad;
    	
    	if((me.State & isBad).any()) std::cout << "Bad" << '\n';
    	if((me.State & isMad).any()) std::cout << "Mad" << '\n';
    }

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    1,626
    This works for me (in C++14 so that the binary literals are legal) :
    Code:
    #include <iostream>
    #include <bitset>
    
    
    using State = std::bitset<4>;
     
    struct Person {
        static const State isBad, isSad, isMad, isFat;
    
    
        const char* name;
        int age;
        State state;
    
    
        Person(const char* name, int age, State state)
            : name(name), age(age), state(state) {}
    
    
        void ShowState() {
            if((state & isBad).any()) std::cout << "Bad\n";
            if((state & isSad).any()) std::cout << "Sad\n";
            if((state & isMad).any()) std::cout << "Mad\n";
            if((state & isFat).any()) std::cout << "Fat\n";
        }
    };
    
    
    const State Person::isBad{ 0b0001 },
                Person::isSad{ 0b0010 },
                Person::isMad{ 0b0100 },
                Person::isFat{ 0b1000 };
    
    
    int main() {
        Person me("Joe", 25, State{ Person::isBad | Person::isMad });
        me.ShowState();
    }
    A little inaccuracy saves tons of explanation. - H.H. Munro

  3. #3
    Registered User
    Join Date
    May 2008
    Posts
    115
    Wow, thank you for fixing the code, I really appreciate it!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how do I pass in a string to the object's constructor
    By sonicflare9 in forum C++ Programming
    Replies: 3
    Last Post: 04-20-2018, 06:23 AM
  2. How to bitset in C?
    By Zacariaz in forum C Programming
    Replies: 17
    Last Post: 05-29-2015, 09:24 AM
  3. Bitset
    By Kristyy_mariee in forum C++ Programming
    Replies: 2
    Last Post: 03-03-2012, 09:00 AM
  4. Bitset help
    By Kristyy_mariee in forum C++ Programming
    Replies: 1
    Last Post: 03-02-2012, 10:33 PM
  5. Bitset questions
    By serge in forum C++ Programming
    Replies: 17
    Last Post: 05-07-2008, 02:00 PM

Tags for this Thread