初始化联合体
以前的代码:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您不应该为此使用 char 数组 - 将
in_addr
和in6_addr
结合起来:要设置 ipv4 环回地址:
对于 IPv6:
You shouldn't use char arrays for this - make a union out of
in_addr
andin6_addr
:To set an ipv4 loopback address:
For IPv6:
bdonlan 的答案很好,但如果你想要完全 POSIXly 可移植的东西,请参阅 getaddrinfo()< /a>. (无论如何,现代 POSIX。)
bdonlan's answer is good, but if you want something totally POSIXly portable, see getaddrinfo(). (Well, modern POSIX, anyway.)
在我看来,这样的联合没有意义,因为
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 ?
由于您(显然,根据此问题附加的标签来判断)在该项目中使用 c++,因此您根本不应该使用联合。 C 使用联合作为一种原始多态性。在 C++ 中,继承是可用的,并且是一种更健壮的实现多态性的方法。
我建议您为此使用小型继承层次结构:
(注意:这是骨架代码,只是为了让您了解它是如何工作的)
这种方法通常是 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)
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!