Friend class read only access C++

132 Views Asked by At

My question is somewhat similar to the title of friend class with limited access. But the limited access I define by 'access-only'. I mean, I've a main class which does the work and creating a helper class which is useful for displaying the internals for debugging purposes. So, inside Main I defined Helper as friend.

Now I want the Helper class to just have read only access to private variables and not write access. Is it possible?

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

Now I have my helper class.

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_;
}
0

There are 0 best solutions below