TITLE: private interface pattern [ This material is based on James Newkirk's article "Private Interface" which can be found at the OMA home page at http://www.oma.com See the original article for a more detailed discussion. ] There are many interesting uses for private inheritance. Here is one such use. Suppose that you have a class called Foobar with a method called tinker() that is private. Suppose you have a class called FoobarFriend that needs access to tinker() as follows: class Foobar { ... private: void tinker (); ... }; class FoobarFriend public: ... void execute () { fb->tinker (); // fails to compile } private: Foobar* fb; }; One simple way to solve this problem is to declare FoobarFriend to be a friend of Foobar. This seems ok since assumedly FoobarFriend is a closely related class to Foobar. However, this could be problematic since all of Foobar's private methods and data are available to FoobarFriend, and could be an invitation to trouble for maintenance programmers. It would be really nice to allow FoobarFriend to access just the tinker() method without making tinker() public. The Solution ------------ Examination of the following code will show such a solution. Note the key use of private inheritance of the class ITinkerable and the usage of ITinkerable in the FoobarFriend class. The interface was used to decouple the Foobar and FoobarFriend classes. Nifty, heh? class ITinkerable { public: virtual tinker () = 0; ... }; class Foobar : private ITinkerable { private: Foobar () { // example FoobarFriend association gFriend.setTinkerable (this); // global example } virtual void tinker (); ... }; class FoobarFriend public: ... void execute () { t->tinker (); } void setTinkerable (ITinkerable* tinker) { t = tinker; } private: ITinkerable* t; // No more Foobar type here };