对象向量-编辑元素和优化

Vector of objects - editing element and optimization

我是cpp新手,想请教几个问题。 首先是我的代码:

main.cpp:

int main()
{
    App app;
    app.Start();
    return 0;
}

App.cpp

void App::Start()
{
    Doc doc;
    Element TempElement;
    for (;;)
    {
        // loop which runs menu and so on
        // Adding new element to vector:

        cout << "Input data" << endl;
        cin >> temp_string;
        TempElement.Set_some_data(temp_string);
        doc.Add_item(&TempElement);
    }
}

Doc.cpp

vector <Element> MyElements;   //before any methods

void Doc::Add_item(Element *TempElement)
{
    MyElements.push_back(*TempElement); 
}

Element.cpp 基本上是 class 我想将哪些对象存储在向量中(在 class 文档中)

有效,但我有几个问题:

  1. 这个方法够用吗?我应该把 "vector MyElements" 放在任何方法之前吗?
  2. 有没有办法在 doc 对象中创建临时元素并在 App 中使用它?
  3. 我试图为我的矢量创建编辑功能,但我失败了。只是为了编辑对象中的一些数据。你有什么建议吗?

我会改变

void Doc::Add_item(Element *TempElement)
{
    MyElements.push_back(*TempElement); 
}

void Doc::Add_item(Element const& tempElement)
                   //     ^^^^^^^ Use const& instead of pointer.
{
    MyElements.push_back(tempElement); 
}

并将调用相应地更改为:

doc.Add_item(TempElement);