1

所以我有以下三个课程:

人员类:

#ifndef INC_7__LABOR_PERSON_H
#define INC_7__LABOR_PERSON_H
#include <string>
#include <iostream>

using namespace std;

class Person{
protected:
    string lastName;
    string firstName;
    int dateOfBirth;
public:
    Person(const string &lastName, const string &firstName, int dateOfBirth);
    virtual void print(ostream& os = cout) const; //a virtualtol polimorfikus lehet.
};

ostream& operator <<(ostream& os, const Person&);

#endif //INC_7__LABOR_PERSON_H

Person 的构造函数:

Person::Person(const string &lastName, const string &firstName, int dateOfBirth) : lastName(lastName),
                                                                                   firstName(firstName),
                                                                                   dateOfBirth(dateOfBirth) {}

员工类:

#ifndef INC_7__LABOR_EMPLOYEE_H
#define INC_7__LABOR_EMPLOYEE_H
#include <iostream>
#include <string>
#include "Person.h"
using namespace std;


class Employee : public Person{
protected:
    string position;
    static int counter;
    int id;
public:
    Employee(const string &lastName, const string &firstName, int dateOfBirth, const string &position);

    Employee(const string &lastName, const string &firstName, int dateOfBirth, int id);

    virtual void print(ostream& os = cout) const;

    int getId() const;
};


#endif //INC_7__LABOR_EMPLOYEE_H

Employee 的构造函数:

Employee::Employee(const string &lastName, const string &firstName, int dateOfBirth, const string &position)
        : Person(lastName, firstName, dateOfBirth), position(position), id(counter++){}

经理类:

#ifndef INC_7__LABOR_MANAGER_H
#define INC_7__LABOR_MANAGER_H
#include "Employee.h"
#include <vector>
using namespace std;


class Manager : public Employee{
private:
    vector<Employee*> employees;

public:

    Manager(const string &lastName, const string &firstName, int dateOfBirth, const string &position);
    static string MANAGER_POSITION;
    virtual void print(ostream& os = cout) const;
    void addEmployee(Employee*);
    void deleteEmployee(int);
};




#endif //INC_7__LABOR_MANAGER_H

Manager 的构造函数:

string Manager::MANAGER_POSITION="manager";

Manager::Manager(const string &lastName, const string &firstName, int dateOfBirth, const string &position)
        : Employee(lastName, firstName, dateOfBirth, position) {}

现在我的问题是我无法通过管理器访问 id 变量:

void Manager::deleteEmployee(int id)
{
    for(int i = 0; i < employees.size(); ++i)
    {
        if(employees.at(i)->id == id) //HERE the "employees.at(i)->id" is the problem.
        {
            employees.erase(employees.begin() + i);
        }
    }
}

尽管 id 变量受到保护,但程序无法访问它。我通过使用 getter getId() 解决了这个问题,但我想知道为什么没有 getter 就无法访问它。

该程序是用 cygwin 编译器用 c++14 编写的。

4

1 回答 1

1

简短的回答是,这是因为 C++ 中的继承就是这样工作的。

您的Manager类正试图访问Employee它继承protected自的类的成员Person。它是手头上的一个单独 Employee对象,它正试图获取它的受保护成员。

protected不能从其他类访问的东西。确实Manager继承自,Employee但这仅意味着 仅在其自己的类中,可以访问它继承的所有和字段。Managerprotectedpublic

您的代码示例可以简化为更短的、简单的、单个可编译的示例(请在阅读 stackoverflow 对最小可重现示例的要求之后,在下一个问题中做同样的事情,因为这将帮助其他人更好地理解您的问题):

class a {
protected:
    int b;
};

class c : public a {

};

class d : public c {

public:
    int method();
};

c *p;
d *q;

int d::method()
{
    return b;    // This is ok
    return p->b; // This is an error
    return q->b; // But this is ok
}

即使d继承自c,它们仍然是两个不同的类,并且如果d正在访问不同 c的对象,则无法访问其受保护的成员。

于 2020-11-04T13:05:48.250 回答