Search code examples
c++encapsulation

access function alternatives without breaking encapsulation


Is there any other way or shorter alternatives to access private base class members in a derived class other than writing a bunch of access functions or overloading operators?


Solution

  • No, if you want true encapsulation of your base class it is recommended to make members private and only allow access via functions. This allows you to do things like preserve invariants.

    But you should only provide access to members you need to. Don't just add accessors for everything without thought. If you find you do need to provide access to everything you might want to rethink your design.

    If you are willing to sacrifice encapsulation you can make the members protected of course.