【问题标题】:The purpose of union with only one member inside the struct结构内只有一个成员联合的目的
【发布时间】:2020-11-23 12:31:55
【问题描述】:

我在Linux内核源代码struct cma_multicast中的纯代码中看到了这样的结构定义内部联合(这不是唯一的一个地方。似乎这是一些常见的做法):

struct cma_multicast {
    struct rdma_id_private *id_priv;
    union {
        struct ib_sa_multicast *ib;
    } multicast;
    struct list_head    list;
    void            *context;
    struct sockaddr_storage addr;
    struct kref     mcref;
};

但我无法弄清楚结构内只有一个成员的联合的目的是什么?为什么我们不能直接输入struct ib_sa_multicast *ib;

我读过这个post,但它没有使用说明,只有 C++ 特性。

UPD:
从 Linux 内核而不是专有代码发布示例。

【问题讨论】:

标签: c c data-structures struct linux-kernel union


【解决方案1】:

这表示在这种情况下多播只能有一个多态调度程序。

它是用C语言编写的面向对象编程。保留联合只是为了命名的统一。

【讨论】:

    猜你喜欢
    • 2014-01-12
    • 1970-01-01
    • 2015-03-09
    • 2014-12-21
    • 2015-01-30
    • 1970-01-01
    • 2014-02-13
    相关资源
    最近更新 更多