0

I'm working on legacy code which looks like the following:

class Foo {
  public:
    Foo();
  private:
    bool a1, a2, a3 /*, ...*/, a50;
};

Foo::Foo() {
  a1 = a2 = a3 /* = ... */ = a50 = false;
}

This is messy. Is there a way to default all private variables of the same time to a single value that's different from the above? I don't want to use an initializer list because there are so many variables.

I know the default constructor of bool assigns false - can this be leveraged?

erip
  • 16,374
  • 11
  • 66
  • 121

7 Answers7

2

There are many possible ways to do it, but all of them are very similar. Anyway you will assign each your variable using different forms.

The main method which I think the best is right assign all variables at your constructor line by line. May be its not compact, but it the most meaningful and you allways can easy look your variables default value:

Foo::Foo() {
    a1 = false;
    a2 = false;
    /*...*/
    a50 = false;
} 

Another method is which you described, with assign operators:

Foo::Foo() {
    a1 = a2 = a3 /* = ... */ = a50 = false;
}

And another one allows initialize variables right after constructor declaration:

Foo::Foo() : 
    a1(false),
    a2(false),
    /*...*/
    a50(true)
    { }

If I forget any method write it to comments, please.

maxteneff
  • 1,523
  • 12
  • 28
  • You didn't mention the much cleaner solution to use an array `a[50] { false };` – PC Luddite Oct 07 '15 at 18:54
  • I do but read the comments to the question. There the author said that he intrested in exactly variable assignment which had meaningful names. As I understand names of variables in question selected for example. – maxteneff Oct 07 '15 at 18:57
1
class Foo
{
private:
    bool a1{}, a2{}, /*...,*/ a50{};
};
AlexD
  • 32,156
  • 3
  • 71
  • 65
0

try with this

Foo::Foo (bool aa) : a1 (aa) , a2 (aa), a3 (aa),/*......*/a50(aa){}
NathanOliver
  • 171,901
  • 28
  • 288
  • 402
0

You can have another class (in a separate header) which looks like following.

class myBool {
  public:
    myBool(int x = 1) { _m = x; }
    operator bool() const { return  0 < _m; }
  private:
    int _m;
};

and in your file you can add following

#include "myBool.h"
#define bool myBool

This will initialize all of bool to default value you set in myBool. You may need to add some more methods to myBool class to use it as a full fledge data type. Above is the bare minimum to explain the answer.

vikrant
  • 393
  • 4
  • 15
0

Here is an alternative solution to the ones I've seen posted so far, in case it's useful to you.

Put the data you want to mass-initialize to a default false/0 value in its own struct:

struct MyData
{
    bool a, b, c, d;
    std::string e, f;
};

Now inherit (privately or otherwise) from this struct, and explicitly initialize it in the constructor's initialization list:

class MyClass : private MyData
{
public:
    MyClass()
        : MyData()
    {

    }
};

This sets all the bools to false, the strings are empty, any ints become 0, pointers become null, etc, etc

If you forget to put the struct explicitly in the initialization list, some of its members may be uninitialized.

Neil Kirk
  • 21,327
  • 9
  • 53
  • 91
0

Confirming that it always requires more work to be lazy in c++...

#include <iostream>
#include <utility>


template<class Tuple, std::size_t...Is>
void zero_out_impl(Tuple& t, std::index_sequence<Is...>)
{
    using expand = bool[];
    (void) expand { false, (std::get<Is>(t) = false)... };
}

template<class...Args>
void zero_out(std::tuple<Args...> t)
{
    zero_out_impl(t, std::index_sequence_for<Args...>());
}

struct lots_of_bools {
    lots_of_bools()
    {
        zero_out(std::tie(a,b,c,d,e,f,g,h,i,j));
    }
private:

    bool a,b,c,d,e,f,g,h,i,j;
};

auto main() -> int
{
    lots_of_bools x;
    return 0;
}
Richard Hodges
  • 68,278
  • 7
  • 90
  • 142
0

Here's another way - wrap the bool in a wrapper that default-constructs it.

#include <iostream>


struct auto_false
{
    auto_false(bool initial = false) : value(initial) {};
    operator bool() const { return value; }
    operator bool& () { return value; }
private:
    bool value;
};

struct lots_of_bools {
    lots_of_bools()
    {
    }

    bool value_of_f() const {
        return f;
    }

    void set_f(bool val) {
        f = val;
    }

private:

    auto_false a,b,c,d,e,f,g,h,i,j;
};

using namespace std;

auto main() -> int
{
    lots_of_bools x;
    cout << x.value_of_f() << endl;
    x.set_f(true);
    cout << x.value_of_f() << endl;
    return 0;
}

output:

0
1
Richard Hodges
  • 68,278
  • 7
  • 90
  • 142