what I am trying to do is an example below.
let's first define a bool.
bool cat = {false};
lets make a fake bool here.
bool setcat(bool booltoset)
{
booltoset = true;
return booltoset;
}
now lets call it with cat.
printf("cat is %s", cat?"true":"false"); //set cat as false.
my question is; is it possible to actually pass a bool through an argument than set that bool?
You need to pass by reference, i.e.:
void setcat(bool& booltoset)
{
booltoset = true;
}
Any function argument is just a variable with scope identical to the function body. If it's an ordinary automatic variable, then changing it has not effect on the caller. This is sometimes useful: you can actually use the arguments, for example:
template<typename F>
void for_each(noexcept_it i, const noexcept_it end, const F &f) noexcept(noexcept(f))
{
for(; i!=end; ++i) f(i); // use i as iteration variable.
}
though the compiler will optimise such things anyway in most cases.
来源:https://stackoverflow.com/questions/18175280/passing-a-bool-as-a-param-c