栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

【C语言】anonymous/unnamed struct&&union

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【C语言】anonymous/unnamed struct&&union

今天学习muduo代码时,发现在类中如下的方式声明了成员变量:

 private:
  union
  {
    struct sockaddr_in addr_;
    struct sockaddr_in6 addr6_;
  };

在类的实现中,直接作为了成员变量使用了addr_ 和 addr6_。

后来经过调查,发现了anonymous/unnamed struct&&union,引用网上一段描述:
Anonymous Union and Structure in C. In C11 standard of C, anonymous Unions and structures were added. Anonymous unions/structures are also known as unnamed unions/structures as they don't have names. Definition is just like that of a normal union just without a name or tag.
翻译:
C中的匿名联合体及结构体。在C11标准中,追加匿名联合体或结构。匿名联合体或结构体由于没有名字也就是无名联合体或结构体。定义就像正常的联合体,只是不带有name或tag。

以前都是声明了一个struct/union为某个类型,然后用该类型创建变量。

typedef union
{
    int a;
    float b;
} union_type;

union_type var;

以上anonymous的方式,在类的对象中可以直接使用 obj.addr_ 或者 obj.addr6_。

写了一个测试程序来说明使用方式,这样看起来更加直观。

#include 
#include 

struct A1
{
    union   // 4
    {
        char c;
        short s;
        int i;
    } __type;
};

struct A2
{
    union   // 8
    {
        int i;
        long long l;
    };
};

struct A3
{
    struct  // 4+8 => 8+8
    {
        int st_i;
        long long st_ll;
    };
};

struct A12  // 4+8 => 8+8
{
    union   // 4
    {
        char c;
        short s;
        int i;
    } __type;

    union   // 8
    {
        int i;
        long long l;
    };
};

struct A13  // 4+ 8+8 => 8+ 8+8
{
    union   // 4
    {
        char c;
        short s;
        int i;
    } __type;

    struct  // 4+8 => 8+8
    {
        int st_i;
        long long st_ll;
    };
};

struct A23 // 8+ 8+8
{
    union   // 8
    {
        int i;
        long long l;
    };

    struct  // 4+8 => 8+8
    {
        int st_i;
        long long st_ll;
    };
};

struct A  // 4 + 8 + 8+8 => 8 + 8 + 8+8
{
    union   // 4
    {
        char c;
        short s;
        int i;
    } __type;

    union   // 8
    {
        int i;
        long long l;
    };

    struct  // 4+8 => 8+8
    {
        int st_i;
        long long st_ll;
    };
};

struct B
{
    union   // 4
    {
        char c;
        short s;
        int i;
    } __type;

    union   // 8
    {
        int i;
        long long l;
    };

    struct  // 4+8 => 8+8
    {
        int st_i;
        long long st_ll;
    };

    struct __NamedStruct
    {
        int type_st_i;
        long long type_st_ll;
    };
};


int main()
{
    struct A a;
    a.__type.i = 4;
    printf("a.__type.i=%dn", a.__type.i);

    printf("assign anonymous union.n");
    a.i = 5;
    printf("a.__type.i=%dn", a.__type.i);
    printf("a.i=%dn", a.i);

    printf("assign anonymous structure.n");
    a.st_i = 7;
    a.st_ll = 15;
    printf("a.__type.i=%dn", a.__type.i);
    printf("a.i=%dn", a.i);
    printf("a.st_i=%dn", a.st_i);
    printf("a.st_ll=%lldn", a.st_ll);

    printf("sizezof(struct A)=%lun", sizeof(struct A));        // 32
    printf("sizezof(struct B)=%lun", sizeof(struct B));        // 32

    printf("sizezof(struct A1)=%lun", sizeof(struct A1));      // 4
    printf("sizezof(struct A2)=%lun", sizeof(struct A2));      // 8
    printf("sizezof(struct A3)=%lun", sizeof(struct A3));      // 16
    printf("sizezof(struct A12)=%lun", sizeof(struct A12));    // 16
    printf("sizezof(struct A13)=%lun", sizeof(struct A13));    // 24
    printf("sizezof(struct A23)=%lun", sizeof(struct A23));    // 24

    return 0;
}

通过以上的赋值及其占用空间大小 ,可以发现对应的对象已经创建。

但是如果在内部声明了一个类型,没有创建该类型的对象,并不占用整个类型的空间(参考struct B与size A的sizeof)。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/965918.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号