简体   繁体   English

如何实例化对象并让它们在 scope 之外仍然可用? (例如在循环中实例化一个 object)

[英]How do I instantiate objects and have them still be available outside of their scope? (e.g. instantiated an object in a loop)

I'm trying to write a program that takes in user input and uses it to create objects that are then pushed to a list.我正在尝试编写一个程序,该程序接受用户输入并使用它来创建对象,然后将其推送到列表中。 Here is a simplified version of what I wrote:这是我写的简化版本:

int main(){
  cout << "How many lists? \n";
  int numLists;
  cin >> numLists;
  list<MyObject> objectList;

  for(int i = 0; i < numLists; i++){
    cout << "Enter some input for list #" << i << "! \n";

    int n = 3;

    while(n-- != 0){
      int input;
      cin >> input;
      MyObject object(input);
      objectList.push_back(object);
    }
    //Save the newly created list somewhere
  }
  //Do something with all the created lists
}

Once the loop finishes, the objects created inside of the loop end up out of scope and the list has size zero.循环完成后,在循环内创建的对象最终会超出 scope 并且列表的大小为零。

I started using我开始使用

list<MyObject> *list = new list<MyObject>
...
MyObject *object = new MyObject(input);
...
(*list).push_back(*object);

and it seems to be working fine, but I was wondering if there was a better way to achieve this.它似乎工作正常,但我想知道是否有更好的方法来实现这一点。 Furthermore, since I am using multiple instances of list and MyObject outside of the loops from which they were created, how do I clear the memory?此外,由于我在创建它们的循环之外使用 list 和 MyObject 的多个实例,我如何清除 memory?

Thank you谢谢

Read up on scope for C++ here: https://en.cppreference.com/w/cpp/language/scope在此处阅读 scope 的 C++: https://en.cppreference.com/w/cpp/language/scope

Why do you want to reference objects made in the for loop?为什么要引用在 for 循环中创建的对象? They have already been copied into the list<> object, so you can read/modify them there.它们已经被复制到列表<> object,所以你可以在那里阅读/修改它们。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 命令模式:如何执行连续动作? (例如,移动物体) - Command pattern: how can I do continuous actions? (e.g. moving an object) 具有相同名称但不同 scope(例如 foo、bar::foo)的命名空间如何工作? - How do namespace's with same name but different scope (e.g. foo, bar::foo) work? 如何添加成员 function 作为例如 Boost Log 过滤器? - How do I add a member function as e.g. Boost Log filter? 如何使用模板 arguments 的特定格式定义 class 模板? 例如:Fn(Args…) - How do I define a class template with specfic format of taking template arguments? e.g.: Fn(Args…) 我如何从类外部(例如,通过另一个类)使用与boosed_graph有关的模板化typedef,即_in_一个类 - How can I use templated typedefs, that are _in_ a class, from outside the class (e.g. by another class), in relation to boost::graph 标准是否要求自动存储中的对象具有任何类型的正确对齐方式(例如,malloc确实如此)? - Does the standard require that objects in automatic storage have the correct alignment for any type (e.g. as malloc does)? 当播放器在多行文本框中更改文本 cursor position 时,我如何调用代码(例如函数)? - How can I have code called (e.g., a function) when the player changes text cursor position in a multiline text box? 检测操作系统是否可用(例如 std::mutex) - Detect if OS is available (e.g. std::mutex) 如何访问(例如,cout)多维 STL 向量中的迭代器的当前值(C++) - how do I access (e.g., cout) the current value of iterators in multi-dimensional STL vectors (C++) 我如何在Linux性能下获得libc6符号(例如_int_malloc)的致电父母? - How do I get call parents for libc6 symbols (e.g. _int_malloc) with linux perf?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM