gpt4 book ai didi

c++ - 派生的公共(public)类不继承 protected 变量

转载 作者:行者123 更新时间:2023-12-03 07:22:32 25 4
gpt4 key购买 nike

所以我有以下三个类(class):
人员类:

#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 编写的。

最佳答案

简短的回答是,这是因为 C++ 中的继承就是这样工作的。
您的 Manager 类正在尝试访问 Employee 类的成员,该类从 protected 继承为 Person 。它是一个 单独的 Employee 对象,它正在尝试获取其 protected 成员。
不能从其他类访问 protected 的东西。确实 Manager 继承自 Employee ,但这仅意味着 Manager 仅在其自己的类 中,可以访问它继承的所有 protectedpublic 字段。
您的代码示例可以简化为更短的、简单的、单个可编译的示例(请在阅读 stackoverflow 对 minimal reproducible example 的要求后,在下一个问题中做同样的事情,因为这将帮助其他人更好地理解您的问题):

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 对象,它就无法访问其 protected 成员。

关于c++ - 派生的公共(public)类不继承 protected 变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64680385/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com