关于向量,联合和指针在C ++中的问题 [英] Questions about vector, union, and pointers in C++

查看:115
本文介绍了关于向量,联合和指针在C ++中的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有的问题不是家庭作业问题,但我正在考虑在我的作业中使用这些概念。上下文,如果它有帮助,就像这样:我需要跟踪几个联合实例,他们属于我自己的联合在我自己的类之一作为类变量。 (注意:联合实例的数量是未知的,因此我不能只有固定数量的联合实例。


  1. 如果我有一个工会,说MyUnion,
    和许多这个联盟的例子,
    然后我可以把它们放到一个向量

     向量< union MyUnion> myVector(10); 


  2. Q2:有效的是有一个
    union的指针吗?

      union MyUnion * myUnionPtr = new Q3:我正在考虑使用一个向量
    的联合指针,这是一个非常简单的方法,因为我们可以使用一个向量

    在我的
    实现,是这个概念
    正确吗?另外,是否是一个正常的
    方法在C + +我需要
    重新思考我的设计吗?

    li>


解决方案


  1. 如果联合是CopyConstructable和Assignable, std :: vector的要求。

  2. 是: MyUnion * ptr = new MyUnion();

  3. 指针容器在某些情况下有效,但如果您想要拥有 指针的容器,请查看Boost的 ptr_ * containers 。但是,这里似乎你会有一个非指针的容器或一个非所有的指针的容器,这是很好的和常见的向量。




默认情况下,所有类型都是CopyConstructable和Assignable。 (可复制用于表示这些概念的并集,但标准单独指定它们。)这是因为除了在某些情况下,将copy ctors和op =添加到类中(联合是一个类类型)。在线有一些参考,但我不知道有一个



您必须以您的方式阻止它,例如:




  • 使复制ctor或op =非公开

  • 使复制ctor或op =使用非const引用

  • 为类类型指定非CopyConstructable或不可分配的成员



示例:

  union CopyableUnion {
int n;
char c;
double d;
};

union NonCopyableUnion {
int n;
char c;
double d;

NonCopyableUnion(){} //必需,因为任何用户定义的ctor,
//如下面的私有拷贝ctor阻止了提供的
// default ctor

private:
NonCopyableUnion(NonCopyableUnion const&);
NonCopyableUnion& operator =(NonCopyableUnion const&);
};

int main(){
CopyableUnion a;
CopyableUnion b = a; // fine,uses copy ctor
b = a; // fine,uses op =

NonCopyableUnion c;
NonCopyableUnion d = c; // compile error(copy ctor)
d = c; // compile error(op =)

return 0;
}




注意:只是因为某些内容是可复制的,并不意味着它会做你想要的!示例:

  struct A {
int * p;

A():p(new int()){}

//提供的拷贝ctor:
// A(A const& other ):p(other.p){}
//被称为member-wise复制

〜A(){delete p; }
};

int main(){
A a;
{
A b = a;
assert(b.p == a.p); // 这是个问题!
} //因为当'b'被销毁时,它会删除同一个指针
//'''保存

return 0; //现在你有未定义的行为当
//〜A尝试再次删除它
}


b $ b

当然,同样的事情也适用于工会。但是,修复同样适用:

  struct A {
int * p;

A():p(new int()){}

A(A const& other)

〜A(){delete p; }
};

(如果你发现它,是的,A有一个问题,如果你试图使用op =其方式与它最初与拷贝ctor相同。)


The questions I have are NOT homework questions, but I am considering using these concepts in my assignment. The context, if it helps, is like this: I need to keep track of several union instances and they belong to my own union in one of my own classes as class variables. (Note: the number of union instances is unknown, so I cannot just have a fixed number of union instances.

  1. Q1: If I have a union, say MyUnion, and many instances of this union, can I then put them into a vector like

    vector<union MyUnion> myVector(10);
    

  2. Q2: Is it valid to have a pointer of union? Like

    union MyUnion *myUnionPtr = new union myUnion;
    

  3. Q3: I am considering using a vector of union pointers in my implementation, is this concept correct? Also, is it a normal approach in C++? Do I need to rethink about my design?

解决方案

  1. If the union is CopyConstructable and Assignable, then it meets the requirements for std::vector.
  2. Yes: MyUnion* ptr = new MyUnion();
  3. A container of pointers works in some situations, but if you want a container of owning pointers, look at Boost's ptr_* containers. However, here it appears you'd either have a container of non-pointers or a container of non-owning pointers, either of which is fine and common for vector.


All types are, by default, both CopyConstructable and Assignable. ("Copyable" is used to mean the union of these concepts, however the standard specifies them individually.) This is because copy ctors and op= are added to classes (unions are one class-type) except under certain circumstances. There are a few references online, but I'm not aware of a single one, available freely online, that spells these out.

You have to go out of your way to prevent it, such as:

  • make the copy ctor or op= non-public
  • make the copy ctor or op= take a non-const reference
  • give the class type a non-CopyConstructable or non-Assignable member

Example:

union CopyableUnion {
  int n;
  char c;
  double d;
};

union NonCopyableUnion {
  int n;
  char c;
  double d;

  NonCopyableUnion() {} // required, because any user-defined ctor,
  // such as the private copy ctor below, prevents the supplied
  // default ctor

private:
  NonCopyableUnion(NonCopyableUnion const&);
  NonCopyableUnion& operator=(NonCopyableUnion const&);
};

int main() {
  CopyableUnion a;
  CopyableUnion b = a; // fine, uses copy ctor
  b = a; // fine, uses op=

  NonCopyableUnion c;
  NonCopyableUnion d = c; // compile error (copy ctor)
  d = c; // compile error (op=)

  return 0;
}


Note: And just because something is Copyable, doesn't mean it does what you want! Example:

struct A {
  int* p;

  A() : p(new int()) {}

  // the provided copy ctor does this:
  //A(A const& other) : p(other.p) {}
  // which is known as "member-wise" copying

  ~A() { delete p; }
};

int main() {
  A a;
  {
    A b = a;
    assert(b.p == a.p); // this is a problem!
  } // because when 'b' is destroyed, it deletes the same pointer
  // as 'a' holds

  return 0; // and now you have Undefined Behavior when
  // ~A tries to delete it again
}

The same thing holds true for unions, of course. The fix applies equally, though, as well:

struct A {
  int* p;

  A() : p(new int()) {}

  A(A const& other) : p(new int(*other.p)) {}

  ~A() { delete p; }
};

(If you spotted it, yes, A has a problem if you ever try to use op=, in just the same way as it originally had with the copy ctor.)

这篇关于关于向量,联合和指针在C ++中的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆