Sorry really poor example of my variable names I just threw them in quick, ill change the OP.
Well basically I am going to have 10 - 20 different bool variables. I will have 2 or 3 different actions, rather than do them all individually I would like to do something like this:
1 2 3 4 5 6 7 8 9 10 11 12
//Will subtract if any these bools are true (lets say these as a group would be called groupSubtract)
//groupSubtract (if there is a variable or function to group these)
bool one = false;
bool five = false;
bool seven = true;
//Will devide if any of the bools are true (lets say these as a group would be called groupDevide)
//groupDevide
bool nine = false;
bool twelve = true;
bool eight = false;
bool three = false;
I want to be able to say
1 2 3
if (groupSubtract == true){
//actions will happen if ANY of the bools in groupSubtract are true
}
Not the way you are trying. You might want to look into std::bitset though, it can do what you want. Of course then you don't have individual names for each boolean.
Ah damn :( obviously the example I gave was just using simple names of numbers to demonstrate my point , I really need individual bool names within my program as I will have tons of conditions based on them individually , "grouping" them was my solution but it appears as if that is not really an option :(.
I guess a work around is to write a for loop with a ton of || statements and just have a bool groupname switch to true if one of them is true, although thats kinda messy , hmmm
A bool has just two values, true or false. Though usually these use a whole byte each, technically, it needs just a single bit. You could use the 32 individual bits of an integer. But that would likely end up with code which is pretty hard to understand.
Perhaps a better solution would be to define a class (or struct) to hold all of the bool values. Then you could use friendly names for each variable, and suitable member functions to test groups of values. This way, the main code becomes simplified as the member functions take care of all the complicated stuff.
Edit:I see Andy has already suggested the use of a class, which I think is a good idea.
1 2 3
if (ex.groupSubtract() == true) {
// etc.
}
the test == true is not really needed, you could just put:
> If I had a list of 5 booleans.
> Is there anyway I can put them into a group ? so I could say ...
> So if just one of the booleans in that group is true it will take action?
We can represent each boolean value as a bit in a std::bitset<>
#include <iostream>
#include <bitset>
int main()
{
constexpr std::size_t NBOOLS = 5 ;
// http://en.cppreference.com/w/cpp/utility/bitset
std::bitset<NBOOLS> group ; // group of 5 bits (or 5 boolean values)
auto zero = group[0] ;
auto one = group[1] ;
auto two = group[2] ;
auto three = group[3] ;
auto four = group[4] ;
two = true ;
three = true ;
std::cout << std::boolalpha ;
for( std::size_t i = 0 ; i < NBOOLS ; ++i )
std::cout << "group[" << i << "] == " << group[i] << '\n' ;
std::cout << "group: " << group << '\n' ;
// is any of the boolean values true? (is any bit set?)
if( group.any() ) std::cout << "atleast one of the " << group.size() << " is true (set)\n" ;
// are all of them false? (is no bit set?)
if( group.none() ) std::cout << "none of the " << group.size() << " are true (set)\n" ;
// how many are true (set)?
std::cout << group.count() << " of the " << group.size() << " are true (set)\n" ;
zero = true ; // set zero to true
three = false ; // set three to false
four.flip() ; // toggle four
std::cout << "group: " << group << '\n' ;
std::cout << group.count() << " of the " << group.size() << " are true (set)\n" ;
// etc.
}
EDIT: On second thoughts, this should not work. (Even though it does on my implementation):
1 2
auto zero = group[0] ;
// etc
IIRC, std::bitset<>::reference does not have an accessible constructor, and we would have to just use the subscript operator instead.
EDIT 2: No, just checked it. auto zero = group[0] ; is just fine. Only the default constructor is declared private; the implicitly defaulted copy-constructor and move constructor would be public.