从类到另一个C ++ OOP获取向量的内容

时间:2017-12-17 17:46:26

标签: c++ oop vector polymorphism

我有一个文件,我在其中创建了一个矢量。在此文件中,我向向量添加内容。然后,我希望能够在不同文件的类中编辑/删除矢量中的内容。我认为继承可以做到这一点,但它并没有复制内容。简化伪:

// parentFile.h

class parentFile {
public:
    void addPerson();
    vector <Income> person_; // Creating vector that gets variables from Income class
};

// parentFile.cpp

void parentFile::addPerson() {  // Add a person to the vector
    Income arg; 
    arg.askInfo();   // Asks string name, last name, float salary
    person_.push_back(arg);    // Adding contents to vector
}


// childFile.h

class childFile : public parentFile {
public:
    void countPersons();
};

// childFile.cpp

void childFile::countPersons()  {  // I need the vector's contents here for editing.
    cout << person_.size() << endl;  // Teste case: person_ vector is empty here. 
}

tl; dr问题:内容添加到矢量person_;在childFile中不再被识别。我一直在试图复制矢量/变形/虚拟函数等,但我还没有取得任何进展。

0 个答案:

没有答案