C++ - 将对象添加到 std::vector,在循环中实例化

标签 c++ collections scope instantiation

我是专家级别的 Java 程序员,正在尝试将我的知识移植到 C++。这不是家庭作业,只是我试图学习 C++ 等价物的概念。

我想做的是使用循环“生成”自定义类型的对象列表。这就是我在 Java 中的做法:

public class TestClass
{
   private ArrayList<ExampleClass> _exampleObjects;
   private int _numObjects = 10;

   public void populateList()
   {
      _exampleObjects = new ArrayList<ExampleClass>();

      for(int i = 0; i < _numObjects; i++)
      {
         _exampleObjects.add(new ExampleClass());
      }
   }

   public void doStuffWithListItems()
   {
      for(ExampleClass e : _exampleObjects)
      {
         e.performAction();
      }
   }
}

super 简单的东西。创建一个列表,遍历任意循环并向其中添加对象。然后,循环遍历这些对象并将它们用于任何目的。

测试类.h:

class TestClass
{
   public:
      // Constructor, copy constructor, destructor definitions

      void populateList();
      void doStuffWithListItems(); 
   private:
      std::vector<ExampleClass> _exampleObjects;
      const int _numObjects = 10;
};

测试类.cpp:

void TestClass::populateList()
{
   for(int i = 0; i < _numObjects; i++)
   {
      ExampleObject obj;
      _exampleObjects.push_back(obj); 

      /* What actually goes here in place of obj? */
   }
}

void TestClass::doStuffWithListItems()
{
   for(auto it = _exampleObjects.begin(); it != _exampleObjects.end(); it++)
   {
      /* What do i do with my iterator to access my object? */
   }
}

据我所知,我在第一个循环中初始化我的对象时,它们会超出范围并在每次循环迭代结束时消亡。那正确吗?如果是这样,我如何制作持久实例?

我对来自的 shared_ptr<> 进行了实验,显然能够持久地存储它们,但我终究无法弄清楚如何从 shared_ptr<> 的迭代器中取消引用。

我觉得这应该是一个非常简单的概念。我就是想不通。我已经阅读了很多关于 C++ 作用域和循环的内容。我似乎在两者上都找不到任何东西。

最佳答案

ExampleObject obj;
_exampleObjects.push_back(obj); 

/* What actually goes here in place of obj? */

没有。你所拥有的是正确的,假设 ExampleClass有一个工作拷贝构造函数。如果您的编译器支持 C++11(并且由于您正在使用 auto,它至少部分支持),您可以为自己保存一份拷贝。

_exampleObjects.emplace_back();

这会在 vector 中就地构造一个对象,将参数(在本例中为无)转发给匹配的构造函数(在本例中为默认构造函数)。要从迭代器访问对象,请执行以下操作:

for(auto it = _exampleObjects.begin(); it != _exampleObjects.end(); it++)
{
   it->performAction();
}

同样,C++11 可以让事情变得更好。

for(auto & obj : _exampleObjects)
{
    obj.performAction();
}

Its my understanding that where I initialise my objects in the first loop, they go out of scope and die by the end of each loop iteration.

正确。

If so, how do I make a persistent instance?

vector<>::push_back照顾这个。它将参数复制到 vector 中。换句话说,它不是在循环中创建的同一个对象,而是一个拷贝。您只需要确保 ExampleClass具有完整的复制语义。

couldn't for the life of me work out how to dereference from an iterator of a shared_ptr<>

如果您有一个指向共享指针 vector 的迭代器(称为 it ),您将取消引用它,并调用存储对象的成员函数,如下所示:

(*it)->performAction();
// alternatively
(**it).performAction();

关于C++ - 将对象添加到 std::vector,在循环中实例化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19779495/

相关文章:

c++ - 在 C++ 中使用 readv()、writev() 和 poll()

java - 创建链接列表的链接列表时出错

c# - BlockingCollection<T> TryTake() 何时可以返回 false?

ruby-on-rails - Rails 4 + Postgresql - 加入具有 2 个条件的表

javascript - 嵌套 JavaScript 函数中的 "this"

c++ - 在连续的内存位置存储字符串文字

c++ - 将 C++ 代码添加到 C 项目

c++ - 您可以将 QWebView cookies/session 与 QNetworkAccessManager 一起使用吗?

java - TreeSet 是否可能等于 HashSet 但 HashSet 不等于 TreeSet

c++ - 将对象引用传递给重载器