静态向量作为类对象容器

时间:2016-03-02 17:29:03

标签: c++

我已经看到很多关于这个问题的问题和答案,但我仍然无法解决我的问题。我应该如何初始化静态向量?我也要求你检查我是否正确使用默认构造。我不是说检查它是否有效,因为我知道它确实有效。我只是想知道它是否是一个优雅的实现?

class Employee
{
private:
    static std::vector<Employee> employee;
    std::string name;
    int age;

    Employee::Employee()
    {
        std::string localName;
        int localAge;

        std::cout << "So... do you want to hire a new employee? Let's look at CVs " << std::endl;
        localName = "Marek"; //GenerateName();
        localAge = 21; //these function is not ready yet. it'd be just a rand()
        std::cout << "I've got one. What do u think about " << localName << " age " << localAge << "?" << std::endl;

        int decision;

        do
        {
            std::cout << "Do you want hire him [1] or not [2] ? " << std::endl;
            std::cin >> decision;

            switch (decision)
            {
            case 1:
                name = localName;
                age = localAge;
                decision = 0;
                break;

            case 2:
                employees.erase(employees.end());
                decision = 0;
                break;

            default:
                std::cout << "Incorrect option. Try again" << std::endl;
            }
        } while (decision != 0);
    }

public:
    static void Employ()
    {
        employees.push_back(Employee::Employee());
    }
};

int main()
{
    Employee::Employ();

    system("pause");
}

2 个答案:

答案 0 :(得分:2)

我运行它时你的代码不起作用。除了添加包含和修复一些拼写错误之外,我还要添加这一行:

std::vector<Employee> Employee::employee;

但是,我不认为这是最好的解决方案。为了清晰起见,员工不应该包含员工的矢量,但应该是员工。如果您想要一个员工矢量,您可以在main(或其他地方)声明一个。如果您希望您的员工矢量具有一些附加功能,例如您编写的交互式员工添加功能,您可以这样做:

class EmployeeForce: public std::vector<Employee>
{
   void interactivelyAddEmployee ();
   ...
};

...
EmployeeForce myStaff;

答案 1 :(得分:0)

我改变了整个概念。问题是我不必要的想要在类中放入对象的容器(在这种情况下:std :: vector)。我现在知道该类应该只包含有关单个对象的信息;它不应该知道关于其他人的任何事情。

class Employee
{
public:
    std::string name;
    int age;

};

std::ostream& operator<<(std::ostream& out, const std::vector<Employee>& v)
{
    out << "[";
    for (size_t i = 0; i < v.size(); ++i)
    {
        out << "Name: " << v[i].name << " age: " << v[i].age;
        if (i != v.size() - 1)
            out << ", ";
    }
    out << "]";

    return out;
}

Employee generate_random_employee(Employee obj)
{
    obj.name = "Marek"; //GenerateName();
    obj.age = 21; //these function is not ready yet. it'd be just a rand()

    return obj;
}

int main()
{
    int decision;
    std::string name;
    std::vector<Employee> employees;
    Employee some_new_employee;

    std::cout << "Welcome mrs. manager. What do you want to do today, sir?" << std::endl << std::endl;

    do
    {
        std::cout << "Hire sombody [1], Fire somebody [2], Exit [0] " << std::endl;
        std::cin >> decision;

        switch (decision)
        {
        case 1:
            some_new_employee = generate_random_employee(some_new_employee);

            if (should_i_employ(some_new_employee))
            {
                employees.push_back(some_new_employee);
            }
            break;

        case 2:
            std::cout << "Who do you want to fire?" << std::endl;
            std::cin >> name;

            if (is_the_employee_exist(employees, name))
            {
                if (are_u_sure())
                {

                }
            }
            else
                std::cout << "" << std::endl;

            break;

        case 3:
            std::cout << employees << std::endl;
            break;

        case 0:
            std::cout << "Good bye, sir" << std::endl;
            break;

        default:
            std::cout << "There is not these option. Try again " << std::endl;
        }

    } while (decision != 0);

    system("pause");
}