如何访问子结构const方法中属性的私有属性



我创建了一个名为Course、具有私有属性std::string code的类,以及另一个名称为Student、具有私有性质std::string id的类。然后我创建了一个名为Enrollment的类作为:

class Enrollment {
private:
Course course;
Student student;
public:
struct EnrHash {
size_t operator() (const Enrollment &__e) const {
auto _code = std::hash<std::string>() (__e.course.code);
auto _id = std::hash<std::string>() (__e.student.id);
return (_code ^ _id);
}
}
}

即使将coursestudent属性更改为protected,我也无法访问它们。我试过用course.getId()替换它,但仍然不起作用。

我想了解为什么会这样,以及如何应对

谢谢:(

使struct EnrHash成为Enrollment:的friend

class Enrollment {
protected:
Course course;
Student student;
public:
friend struct EnrHash;
struct EnrHash {
…
};
}

最新更新