简体   繁体   中英

How to set all struct members to same value?

I have a struct:

struct something {
    int a, b, c, d;
};

Is there some easy way to set all those a,b,c,d into some value without needing to type them separately:

something var = {-1,-1,-1,-1};

Theres still too much repetition (lets imagine the struct has 30 members...)

I've heard of "constructs" or something, but i want to set those values into something else in different part of the code.

This is my second answer for this question. The first did as you asked, but as the other commentors pointed out, it's not the proper way to do things and can get you into trouble down the line if you're not careful. Instead, here's how to write some useful constructors for your struct:

struct something {
    int a, b, c, d;

    // This constructor does no initialization.
    something() { }

    // This constructor initializes the four variables individually.
    something(int a, int b, int c, int d) 
        : a(a), b(b), c(c), d(d) { }

    // This constructor initializes all four variables to the same value
    something(int i) : a(i), b(i), c(i), d(i) { }

//  // More concise, but more haphazard way of setting all fields to i.
//  something(int i) {
//      // This assumes that a-d are all of the same type and all in order
//      std::fill(&a, &d+1, i);
//  }

};

// uninitialized struct
something var1;

// individually set the values
something var2(1, 2, 3, 4);

// set all values to -1
something var3(-1);

Simply give the struct a constructor:

struct something {
    int a, b, c, d;
    something() {
        a = b = c = d = -1;
    }
};

and then use it:

int main() {
   something s;    // all members will  be set to -1
}

you can also use the constructor to reset members:

int main() {
   something s;    // all members will  be set to -1
   s.a = 42;   
   s = something();  // reset everything back to -1
}

You can define a method for the struct. So why not:

struct something {
    int a, b, c, d;

    void set_values(int val) 
    { 
      a = b = c = d = val;
    }
};

something foo;

foo.set_values(-1);

Its definitely worth mentioning the point @sbi raised in the comments: if you're intent is to initialize the struct, then you should do so with a constructor. You should avoid allowing users of your structs/object to put it in an unusable/error state.

As i gathered, you want your struct to stay a POD but still want to have some "convenience constructor".
Adding a constructor to it wouldn't work in this case as you'd lose the POD-ness, thus i'd use a helper function:

something make_something() {
    something s = { -1, -1, -1, -1};
    return s;
}

If you want to set it to varying values, let the function take a, maybe optional, parameter:

something make_something(int i = 0) {
     something s = { i, i, i, i };
     return s;
} 

Now you can get the definition and initialization down to one line:

something s = make_something(-1);

Make a union of your structure, and an array. And use a loop to initialize the array.

union something {
    struct {
       int a,b,c,d;
    };
    int init[4];
};

   something truc;
   for (int i=0; i<4; i++) truc.init[i] = -1;

If you have

struct
{int a,b,c.......;}foo;

I've written that code and it seems to be working fine:

int* pfoo;
for (int i = 0; i < sizeof(foo); i++)
{
    pfoo = (int*)((int)(&foo) + i*sizeof(int));
    *pfoo = f(i/2); //set the values (here: the values of a random function f)
}

It writes the memory directly starting at &foo (the address of the first variable in the struct)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM