简体   繁体   中英

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

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:)

Make struct EnrHash a friend of Enrollment :

class Enrollment {
  protected:
    Course course;
    Student student;

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM