向量指针vs指针向量vs指针向量指针 [英] Pointer to vector vs vector of pointers vs pointer to vector of pointers

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

问题描述

只是想知道您认为关于C ++中向量的最佳实践是什么.

Just wondering what you think is the best practice regarding vectors in C++.

如果我有一个包含向量成员变量的类. 何时应将此向量声明为:

If I have a class containing a vector member variable. When should this vector be declared a:

  1. 整个对象"矢量成员可变,包含值,即vector<MyClass> my_vector;
  2. 指向向量的指针,即vector<MyClass>* my_vector;
  3. 指针的向量,即vector<MyClass*> my_vector;
  4. 指向指针向量的指针,即vector<MyClass*>* my_vector;
  1. "Whole-object" vector member varaiable containing values, i.e. vector<MyClass> my_vector;
  2. Pointer to a vector, i.e vector<MyClass>* my_vector;
  3. Vector of pointers, i.e. vector<MyClass*> my_vector;
  4. Pointer to vector of pointers, i.e. vector<MyClass*>* my_vector;

我在一个类中有一个特定的示例,其中我目前已将向量声明为案例4,即vector<AnotherClass*>* my_vector; 这里的AnotherClass是我创建的另一个类.

I have a specific example in one of my classes where I have currently declared a vector as case 4, i.e. vector<AnotherClass*>* my_vector; where AnotherClass is another of the classes I have created.

然后,在构造函数的初始化列表中,使用new创建矢量:

Then, in the initialization list of my constructor, I create the vector using new:

MyClass::MyClass()
: my_vector(new vector<AnotherClass*>())
{}

在析构函数中,我执行以下操作:

In my destructor I do the following:

MyClass::~MyClass()
{
  for (int i=my_vector->size(); i>0; i--)
  {
    delete my_vector->at(i-1);
  }
  delete my_vector;
}

向量的元素被添加到我的类的方法之一中. 我不知道预先将多少个对象添加到我的向量中.这是在代码执行时根据解析xml文件而决定的.

The elements of the vectors are added in one of the methods of my class. I cannot know how many objects will be added to my vector in advance. That is decided when the code executes, based on parsing an xml-file.

这是好习惯吗?还是应将向量声明为其他情况1、2或3中的一种?

Is this good practice? Or should the vector instead be declared as one of the other cases 1, 2 or 3 ?

什么时候使用哪种情况?

When to use which case?

我知道向量的元素如果是另一个类的子类(多态性),则应该是指针.但是在任何其他情况下都应该使用指针吗?

I know the elements of a vector should be pointers if they are subclasses of another class (polymorphism). But should pointers be used in any other cases ?

非常感谢您!

推荐答案

通常,您需要的是解决方案1,因为它是C ++中最简单的:您不必照顾内存,C ++会为您完成所有这些工作(例如,您无需再提供任何析构函数).

Usually solution 1 is what you want since it’s the simplest in C++: you don’t have to take care of managing the memory, C++ does all that for you (for example you wouldn’t need to provide any destructor then).

在某些特定情况下,这是行不通的(最明显的是在使用多态对象时),但总的来说,这是唯一的好方法.

There are specific cases where this doesn’t work (most notably when working with polymorphous objects) but in general this is the only good way.

即使在使用多态对象或需要堆分配的对象(无论出于何种原因)时,原始指针几乎都不是一个好主意.而是使用智能指针或智能指针容器.现代C ++编译器提供了即将发布的C ++标准中的shared_ptr.如果您使用的尚不具备此功能的编译器,则可以使用

Even when working with polymorphous objects or when you need heap allocated objects (for whatever reason) raw pointers are almost never a good idea. Instead, use a smart pointer or container of smart pointers. Modern C++ compilers provide shared_ptr from the upcoming C++ standard. If you’re using a compiler that doesn’t yet have that, you can use the implementation from Boost.

这篇关于向量指针vs指针向量vs指针向量指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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