如何在c++中动态创建union实例?

发布于 2024-08-12 23:20:06 字数 39 浏览 6 评论 0原文

我需要有多个联合实例作为类变量,那么如何在堆中创建联合实例?谢谢

I need to have several instances of a union as class variables, so how can I create a union instance in the heap? thank you

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

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

发布评论

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

评论(6

倦话 2024-08-19 23:20:06

与创建任何其他对象相同:

union MyUnion
{
   unsigned char charValue[5];
   unsigned int  intValue;
};


MyUnion *myUnion = new MyUnion;

您的联合现在位于堆上。请注意,联合是其最大数据成员的大小。

The same as creating any other object:

union MyUnion
{
   unsigned char charValue[5];
   unsigned int  intValue;
};


MyUnion *myUnion = new MyUnion;

Your union is now on the heap. Note that a union is the size of it's largest data member.

快乐很简单 2024-08-19 23:20:06

我的 C++ 有点生疏,但是:

   my_union_type *my_union = new my_union_type;
   ...
   delete my_union;

My C++ is a bit rusty, but:

   my_union_type *my_union = new my_union_type;
   ...
   delete my_union;
橘味果▽酱 2024-08-19 23:20:06

与结构相同:) 您可以使用 malloc() 并以 C 方式执行,或使用 new 以 C++ 方式执行。秘密在于结构、联合和类是相关的;结构只是一个没有方法的类(通常)。如果您关心的话,以下评论中有更多说明。

Same as a struct :) You can use malloc() and do it the C way, or new for the C++ way. The secret is that structs, unions and classes are related; a struct is just a class (usually) without methods. There's more clarification in the following comments, should you care.

千里故人稀 2024-08-19 23:20:06

使用new 运算符。

Use the new operator.

不及他 2024-08-19 23:20:06

我不确定你想把这个问题带到哪里。联合是一种用户定义的数据或类类型,在任何给定时间,仅包含其成员列表中的一个对象。因此,从这里开始,如果您有一个像这样定义的联合:

union DataType
{
    char ch;
    integer i;
    float f;
    double d;
};

那么您可以使用 DataType 作为类型来定义类中的成员,或者作为类型来定义堆栈上的变量,就像常规类型一样您定义的、结构或类。

I'm not sure where you want to head with this. A union is a user-defined data or class type that, at any given time, contains only one object from its list of members. So starting from this, if you have a union defined like this:

union DataType
{
    char ch;
    integer i;
    float f;
    double d;
};

You can then use DataType as a type to define members in a class or as a type to define variables on the stack, just like regular types, struct or classes you define.

手心的温暖 2024-08-19 23:20:06

使用 new 运算符:

#include <iostream>

union u {
  int a;
  char b;
  float c;
};

class c {
public:
  c() { u1 = new u; u2 = new u; u3 = new u; }
  ~c() { delete u1; delete u2; delete u3; }
  u *u1;
  u *u2;
  u *u3;
};

int main()
{
  c *p = new c;

  p->u1->a = 1;
  p->u2->b = '0' + 2;
  p->u3->c = 3.3;

  std::cout << p->u1->a << '\n'
            << p->u2->b << '\n'
            << p->u3->c << std::endl;

  delete c;

  return 0;
}

Use the new operator:

#include <iostream>

union u {
  int a;
  char b;
  float c;
};

class c {
public:
  c() { u1 = new u; u2 = new u; u3 = new u; }
  ~c() { delete u1; delete u2; delete u3; }
  u *u1;
  u *u2;
  u *u3;
};

int main()
{
  c *p = new c;

  p->u1->a = 1;
  p->u2->b = '0' + 2;
  p->u3->c = 3.3;

  std::cout << p->u1->a << '\n'
            << p->u2->b << '\n'
            << p->u3->c << std::endl;

  delete c;

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