简体   繁体   中英

How to add class member to vector and use it correctly?

Suppose I have this class

class Employee {
string name;
vector<Employee> subEmployee;

Employee(string name, vector<Employee> subEmployee) {
    this -> name = name;
    this -> subEmployee = subEmployee;
}
vector<Employee> getSubEmployee() {
    return this -> subEmployee;
}};

and I create several objects and connect them,

Employee yunfei("yunfei",{});
Employee yuqi("yuqi",{});
Employee yuwei("yuwei",{});
Employee aona("aona",{});
Employee shang("shang",{});
yuqi.addNewEmployee(yuwei);
yuqi.addNewEmployee(aona);
yunfei.addSubEmployee(yuqi);

Then I want to check how many subemployee yunfei has, so I use:

yunfei.getSubEmployee();

The result shows vector contains yuqi, however, when I try to use:

yunfei.getSubEmployee()[0].getSubEmployee();

I just get an empty list, but when I use:

yuqi.getSubEmployee();

I get the correct answer, so I want to know why that happened.

Like most people have mentioned, you want to make use of references to ensure you are always referencing the same employee.

class Employee
{
    using EmployeeList = std::vector<std::shared_ptr<Employee>>;
    std::string mName;
    EmployeeList mSubEmployees;

public:
    Employee(const std::string& name, const EmployeeList& subEmployees = {}) 
        : mName(name), mSubEmployees(subEmployees)
    {}

    const EmployeeList& GetSubEmployees() const { return mSubEmployees; }
    const std::string& GetName() const { return mName; }
    void AddSubEmployee(const std::shared_ptr<Employee>& employee) { mSubEmployees.push_back(employee); }
}

// I can make these employees first.
auto yuwei = std::make_shared<Employee>("yuwei");
auto aona = std::make_shared<Employee>("aona");
auto shang = std::make_shared<Employee>("shang");

// I can construct these employees using existing employees
auto yuqi = std::make_shared<Employee>("yuqi", { yuwei, aona });
auto yunfei = std::make_shared<Employee>("yunfei",{ yuqi });

// I can add new subs now
yunfei.addSubEmployee(shang);

// Even brand new employees
yunfei.addSubEmployee(new Employee("Aesthete");

These employees only exist int the context where you declared them. All other employees now only hold reference to the others. You could now iterate the employee tree using recursion.

void PrintEmployeeTree(const Employee& e, int level = 1)
{
    for (const auto& employee : e->getSubEmployees)
    {
        std::cout << std::string(level, '-') << employee->GetName() << std::endl;
        PrintEmployeeTree(employee, level+1);
    }
}

PrintEmployeeTree(yunfei);

Should give you something like this:

-yunfei
--yuwei
---yuki
---aona
--shang
--Aesthete

Try to use references wherever makes sense. Same goes for const. Notice how even the employee name is return by reference. Look at where all the references I have made are, and how many copies they have avoided. This will ensure you're always working with the same set of data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM