⌨️ Dev Blog

Hi 🖐️ I am Avinash.

  • Welcome to my blog. I write about software here.
  • I write about math here.

C++: Restrict Access To Class Methods From All But Few Outsiders

The Problem Say we have a class ParkingSlot with method Park(). Outside the class, this method should only be accessible to another class ParkingLot. How do we implement this? One (really lazy) way is to make the method private and declare the other class a friend. class ParkingSlot { // omitting other private members void Park(Vehicle *vehicle); friend class ParkingLot; public: // omitting public members }; This works, but now the friend class can access all other private members....

December 2, 2022 · 4 min · Avinash Pandey