初始化联合体

发布于 2024-11-09 15:13:10 字数 586 浏览 3 评论 0原文

以前的代码:

struct Inet_address{
char v4[4];
};
extern "C" Inet_address Inet_loopback = 
{
  {127,0,0,1}
};

修改后:

我已将 Inet_address 设为并集 这里 Inet 地址是一个联合

union Inet_address{
char v4[4];
char v6[16];
};

现在我想对 extern "C" Inet_address Inet_loopback 执行相同的操作 说,

extern "C" Inet_address Inet_loopback = 
{
 if(some condition)
  {127,0,0,1}  //It should be Inet_address.v4
 else
    { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1 }  //This should be Inet_address.v6
};

请建议一个正确的方法来实现这一点,因为我在这里遇到错误。

Previous code :

struct Inet_address{
char v4[4];
};
extern "C" Inet_address Inet_loopback = 
{
  {127,0,0,1}
};

After modifying:

I have made Inet_address a union
Here Inet address is a union

union Inet_address{
char v4[4];
char v6[16];
};

Now I want to do the same operation on extern "C" Inet_address Inet_loopback
Say,

extern "C" Inet_address Inet_loopback = 
{
 if(some condition)
  {127,0,0,1}  //It should be Inet_address.v4
 else
    { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1 }  //This should be Inet_address.v6
};

Please suggest a correct way to acheive this as I am getting error here.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

白色秋天 2024-11-16 15:13:10

您不应该为此使用 char 数组 - 将 in_addrin6_addr 结合起来:

typedef union {
    struct in_addr inaddr;
    struct in6_addr in6addr;
} in46_addr_t;

要设置 ipv4 环回地址:

addr.inaddr.s_addr = INADDR_LOOPBACK;

对于 IPv6:

addr.in6addr = in6addr_loopback; // pre-defined global variable (note: assumes linux socket API)

You shouldn't use char arrays for this - make a union out of in_addr and in6_addr:

typedef union {
    struct in_addr inaddr;
    struct in6_addr in6addr;
} in46_addr_t;

To set an ipv4 loopback address:

addr.inaddr.s_addr = INADDR_LOOPBACK;

For IPv6:

addr.in6addr = in6addr_loopback; // pre-defined global variable (note: assumes linux socket API)
只怪假的太真实 2024-11-16 15:13:10

bdonlan 的答案很好,但如果你想要完全 POSIXly 可移植的东西,请参阅 getaddrinfo()< /a>. (无论如何,现代 POSIX。)

struct addrinfo hints = { 0 };
hints.ai_family = AF_INET6;
hints.ai_flags = AI_NUMERICHOST;
struct addrinfo *result = NULL;

getaddrinfo("::1", NULL, &hints, &result);

// result[0].ai_addr is now a pointer to the localhost IPv6 address as a sockaddr_in6
// struct.

freeaddrinfo(result);

bdonlan's answer is good, but if you want something totally POSIXly portable, see getaddrinfo(). (Well, modern POSIX, anyway.)

struct addrinfo hints = { 0 };
hints.ai_family = AF_INET6;
hints.ai_flags = AI_NUMERICHOST;
struct addrinfo *result = NULL;

getaddrinfo("::1", NULL, &hints, &result);

// result[0].ai_addr is now a pointer to the localhost IPv6 address as a sockaddr_in6
// struct.

freeaddrinfo(result);
空名 2024-11-16 15:13:10

在我看来,这样的联合没有意义,因为 Inet_address 会在内存中占用最大数组的大小。为什么不只使用一个包含 16 个元素的数组?此外,如果您的条件只能在运行时评估,则必须将初始化放入函数中。如果您的条件可以在编译时评估,那么您尝试做的事情可能可以使用模板和元编程来实现,但它不会简单,而且可能很难阅读。

您能告诉我们更多有关您病情的信息吗?

In my opinion it does not make sense to have such union, since Inet_address would take in memory the size of the biggest array. Why don'y you use only one array of 16 elements ? Furthermore, you will have to put your initialization in a function if your condition can only be evaluated during runtime. If your condition can be evaluated during compile time, may be what you try to do could be achieved using templates and metaprogramming, but it won't be simple and it may be hardly readable.

Could you tell us more about your condition ?

盗心人 2024-11-16 15:13:10

由于您(显然,根据此问题附加的标签来判断)在该项目中使用 c++,因此您根本不应该使用联合。 C 使用联合作为一种原始多态性。在 C++ 中,继承是可用的,并且是一种更健壮的实现多态性的方法。

我建议您为此使用小型继承层次结构:
(注意:这是骨架代码,只是为了让您了解它是如何工作的)

class Inet_address
{
    vector<int> _address;

    public:
        Inet_address();
        virtual ~Inet_address();

        // pure virtual function...special behavior implemented in sub classes
        virtual vector<int> loopback() = 0;
}

class Inet_address_v4 : public Inet_address
{
    public:
        Inet_address_v4();
        virtual ~Inet_address_v4();

        // special behavior for v4
        virtual vector<int> loopback();
}

class Inet_address_v6 : public Inet_address
{
    public:
        Inet_address_v6();
        virtual ~Inet_address_v6();

        // special behavior for v6
        virtual vector<int> loopback();
}

int main()
{
    // Create a new v4 address object
    Inet_address* my_v4_addr = new Inet_address_v4();

    // Create a new v6 address object
    Inet_address* my_v6_addr = new Inet_address_v6();

    vector<Inet_address*> addresses;

    addresses.push_back( my_v4_addr );
    addresses.push_back( my_v6_addr );

    // The loopback function is called in the same way for both sub-classes
    for( unsigned int i=0; i<addresses.size(); i++ )
    {
        Inet_address* curr_adr = addresses[i];
        curr_addr->loopback();
    }

    return 0;
}

这种方法通常是 C++ 中使用的标准方法。这是一种非常强大的方法,因为它可以让您创建许多不同类型的具有相似行为的对象,这些对象都可以使用完全相同的函数调用进行操作。

有关 C++ 继承的概述,请参阅:http://www.cplusplus.com/doc/tutorial /继承/

祝你好运!

Since you are (apparently, judging by the tags attached to this question) using c++ for this project, you should not use a union at all. C used unions as a sort of primitive polymorphism. In c++ inheritance is available, and is a much more robust method of implementing polymorphism.

I would recommend that you use a small inheritance heirarchy for this:
( Note: this is skeleton code that is only meant to give you an idea of how this might work)

class Inet_address
{
    vector<int> _address;

    public:
        Inet_address();
        virtual ~Inet_address();

        // pure virtual function...special behavior implemented in sub classes
        virtual vector<int> loopback() = 0;
}

class Inet_address_v4 : public Inet_address
{
    public:
        Inet_address_v4();
        virtual ~Inet_address_v4();

        // special behavior for v4
        virtual vector<int> loopback();
}

class Inet_address_v6 : public Inet_address
{
    public:
        Inet_address_v6();
        virtual ~Inet_address_v6();

        // special behavior for v6
        virtual vector<int> loopback();
}

int main()
{
    // Create a new v4 address object
    Inet_address* my_v4_addr = new Inet_address_v4();

    // Create a new v6 address object
    Inet_address* my_v6_addr = new Inet_address_v6();

    vector<Inet_address*> addresses;

    addresses.push_back( my_v4_addr );
    addresses.push_back( my_v6_addr );

    // The loopback function is called in the same way for both sub-classes
    for( unsigned int i=0; i<addresses.size(); i++ )
    {
        Inet_address* curr_adr = addresses[i];
        curr_addr->loopback();
    }

    return 0;
}

This approach is generally the standard method used in c++. It is a really powerful approach, because it can allow you to create many different types of objects with similar behavior that can all be manipulated using the exact same function calls.

See this for an overview of c++ inheritance: http://www.cplusplus.com/doc/tutorial/inheritance/

Good luck!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文