开发者

What might you use a nameless struct or union for?

开发者 https://www.devze.com 2023-02-03 05:59 出处:网络
I can see how union { uint32_t ip_address struct { uint8_t oct1; uint8_t oct2; uint8_t oct3; uint8_t oct4;

I can see how

union {
uint32_t ip_address
 struct {
          uint8_t oct1;
          uint8_t oct2;
          uint8_t oct3;
          uint8_t oct4;
        };
};
开发者_运维问答

Might be of some use to someone, but struct in a struct example here: Detail of MS warning C4201 seems a bit odd. Can anyone demonstrate a good usage case?


A nameless union inside a struct makes sense because it allows you to refer to the members of the union without specifying its name, hence shorter code:

struct {
  int a;

  union {
    int b, c, d;
  };
} foo;

So accessing the members of the union is just like accessing a member of the containing struct: foo.a and foo.b. Otherwise you have to use foo.union_name.b to access a member of the union.

Of course a "user" programmer using such a struct should be aware that setting foo.c affects the value of foo.b and foo.d.

For the same reason the reverse can be done, namely putting an anonymous struct inside a union:

union {
  struct {
    int a, b;
  };

  int c;
} foo;

This way foo.a and foo.b can be used simultaneously and foo.c can be used in another case.

I can't think of any other uses for anonymous structs or unions. "Declaring" an anonymous struct/union is an oxymoron and is just like saying int; instead of int a;.


This is mainly because struct and union are so similar. In general, it is only useful to have anonymous structs within unions, and anonymous unions within structs, but nesting structs within structs or unions within unions is mostly equivalent to listing their members directly.

Note that unions should not be used to convert values, as there are absolutely no layout guarantees.


You can use an anonymous struct in a struct or class to group some value that are plain old data to ease the initialisation of the instance (with a memset) but can't do it because the class derive from another one.

Exemple:

#include <string.h>
class Base
{
private:
    int field1;
    int field2;

public:
    Base()
    {
        // initialize field1, field2
    }

    virtual void DoSomething();
};

class Derived : public Base
{
private:
    struct
    {
        int field2;
        int field3;
        int field4;
        // ...
        int field99;
    } data;

public:
    Derived()
    : Base()
    {
        memset(&data, 0, sizeof(data));

        // if you didn't group those value in an anonymous struct
        // you would have to do something like that (not safe):
        // memset(sizeof(Base) + (char*)this, 0, sizeof(Derived) - sizeof(Base));
    }
};

Note that there exists better alternatives (in my example using a std::vector or int[99]). But it can sometimes be useful.


In this case your mapping the structure (containing 4 bytes) and the unsigned int (4 bytes also) to the same memory location.

0

精彩评论

暂无评论...
验证码 换一张
取 消