Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to maintain initialization of struct as members are added? [closed]

Tags:

c++

c++03

I have a question that is sort of a follow up to this:

Initializing default values in a struct

I've got a struct that's already got 17 bools, and a clear() method that sets them all to false. It's a long term project; the code could still be in use years from now and get added to. Is there a way to initialize all members that will extend automatically, so that someone adding new members to the struct doesn't need to remember to add them to the clear() method (other than a comment saying "please don't forget")?

This code base is not C++11 at this time, so I don't think I can initialize in the declaration.

The code is like this:

typedef struct {
    bool doThingA;
    bool doThingB;
    bool doThingC;
    bool doThingD;
    // etc for several more bools

    void clear() {
        doThingA = false;
        doThingB = false;
        doThingC = false;
        doThingD = false;
        // etc...
    }
} EnableFlags;
like image 336
Paul Farrington Avatar asked Nov 13 '15 17:11

Paul Farrington


People also ask

How do you initialize struct members?

When initializing an object of struct or union type, the initializer must be a non-empty, (until C23) brace-enclosed, comma-separated list of initializers for the members: = { expression , ... }

Why We Can not initialize member of structure at the time of declaration?

This is because when a structure is defined, no memory is allocated to the structure's data members at this point. Memory is only allocated when a structure variable is declared. Consider the following code snippet. int length = 10; // COMPILER ERROR: cannot initialize members here.

Are struct members initialized to zero?

If a structure variable does not have an initializer, the initial values of the structure members depend on the storage class associated with the structure variable: If a structure variable has static storage, its members are implicitly initialized to zero of the appropriate type.

Does a struct need to be initialized?

Struct initialization and default valuesAll of a struct's member fields must be definitely assigned when it's created because struct types directly store their data. The default value of a struct has definitely assigned all fields to 0.


3 Answers

struct EnableFlags {
    bool doThingA;
    bool doThingB;
    bool doThingC;
    bool doThingD;
    // etc for several more bools

    void clear() {
        *this = EnableFlags();
    }
};

This will create a temporary with all members set to zero and then make *this a copy of it. So it sets all the members to zero, no matter how many there are.

This assumes that you haven't defined a default constructor that does something other than set all the flags to false. If you have no user-defined constructors then that assumption holds.

Since C++11 it's even simpler:

void clear() {
    *this = {};
}
like image 119
Jonathan Wakely Avatar answered Oct 14 '22 13:10

Jonathan Wakely


One option is to use a static assertion about the size of the structure inside the clear function.

First determine the current size of the struct. Let's say it's 68. Then, add this:

void clear()
{
  BOOST_STATIC_ASSERT(sizeof(EnableFlags) == 68 && "You seem to have added a data member. Clear it before changing the number 68 to match current struct size!!");

  // the rest as before ...
}

I've used Boost's static assertion macro, but you can use any other one you want, or roll out your own.

With such an assertion, the code will fail to compile when the size of the structure changes. It's not a catch-all solution, but does provide a safeguard.

like image 39
Angew is no longer proud of SO Avatar answered Oct 14 '22 13:10

Angew is no longer proud of SO


Just use objetcs.

So you can use a 'for' loop to check in a std::vector if their values are false or true.

So you don't have your futurs co-workers put the "false" value each time they create a new boolean variable.

Structures are inapropriates here.

like image 30
Madz Avatar answered Oct 14 '22 14:10

Madz