簡體   English   中英

如何在子結構 const 方法中訪問屬性的私有屬性?

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

我創建了一個名為Course的 class 和私有屬性std::string code和另一個 class 名為Student私有屬性std::string id 然后我創建了一個名為Enrollment的 class 為:

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成為Enrollmentfriend

class Enrollment {
  protected:
    Course course;
    Student student;

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM