How to access a private property of a property in child struct const method?

Viewed 25

I created a class named Course with private property std::string code and another class named Student with private property std::string id. Then I created a class named Enrollment as:

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);
      }
    }
}

I can't access the course and student properties even after changing them to protected. I have tried replacing it with course.getId() but still not working.

I want to understand why that is and how to deal with it.

Thanks :)

1 Answers

Make struct EnrHash a friend of Enrollment:

class Enrollment {
  protected:
    Course course;
    Student student;

  public:
    friend struct EnrHash;
    struct EnrHash {
…
    };
}
Related