3

我的问题有点类似于friend class with limited access的标题。但是我通过“仅限访问”定义的有限访问。我的意思是,我有一个主类来完成这项工作并创建一个辅助类,该类对于显示内部用于调试目的很有用。因此,在 Main 中,我将 Helper 定义为朋友。

现在我希望 Helper 类只具有对私有变量的只读访问权限而不是写访问权限。可能吗?

class Main {
 private:
  // DSs that I don't want to expose it to rest
  // even through getters and setters.
  DataStrcuture ds_;

  // I want a way to specify that no modifications should be
  // allowed through this helper.
  friend class Helper;
}

现在我有我的助手类。

class Helper {
  Helper(Main *main) : main_(main) {
  }

  void ShowStatsAndDebugInfo() const {
    PrettyPrint(main_->ds_);
  }

 private:
  // I want this to always be used like a const variable.
  Main *main_;
}
4

0 回答 0