Path: utzoo!utgpu!watmath!clyde!att!rutgers!ucsd!sdcsvax!ucsdhub!hp-sdd!ncr-sd!ncrlnk!uunet!mcvax!ukc!warwick!rlvd!cmc
From: cmc@inf.rl.ac.uk (Chris Crampton)
Newsgroups: comp.lang.c++
Subject: Re: Can we hide the private part of a class ?
Message-ID: <4368@rlvd.UUCP>
Date: 21 Nov 88 14:44:52 GMT
References: <1358@stratus> <1988Sep29.044111.16104@utzoo.uucp> <4193@polya.Stanford.EDU> <322@pvab.UUCP>
Reply-To: cmc@inf.rl.ac.uk (Chris Crampton)
Organization: Rutherford Appleton Laboratory, Didcot. UK.
Lines: 76

In article <322@pvab.UUCP> robert@pvab.UUCP (Robert Claeson) writes:
>In article <4193@polya.Stanford.EDU>, shap@polya.Stanford.EDU (Jonathan S. Shapiro) writes:
>
>[discussion about how to hide the private part of a class declaration deleted]
>
>> ... And yes, his method included the use of a pointer.

One solution I have used which really is quite straight forward and bound to
have occured to others is as follows:

------------foo.h------------------

class	foo
 {
	struct	foo_privates*	pfoo;	// private data
	
	void	private_method();
	...
public:
	...
 };

------------foo.c------------------

#include	"foo.h"

struct foo_privates
 {
	...	// implementation dependant data and methods
 };

 ...		// implementation code

-----------------------------------

The file foo.h is included by all users of the foo class and foo.c is the
implementation module for class foo. It is only in the latter that the
private data of foo is described, thus hiding any details. Only foo.h
need be distributed with the library providing the foo class.

If, of course, the parts of the implementation that need access to the
private data are spread across more than one C++ file then the private
data would have to be described in additional header file (which needn't
be distributed).

There are two advantages to this scheme:

	- the details of the implementation are kept private

	- if the implementation is changed but the interface remains
	  unchanged then the amount of recompilation necessary is
	  greatly reduced

There are disadvantages:

	- private methods become messy in that they aren't part of the
	  actual class or, if they are, then they are exposed in the header
	  file (as in private_method() above)

	- when debugging classes using debuggers intended for old C then
	  the private struct probably has to be moved into the header file
	  so that complete debugging can be performed

	- inline funcions which use private data are not possible

You pays yer money...

Chris.

=======================================================================
Chris M Crampton		JANET:	cmc@uk.ac.rl.inf
Informatics Department		ARPA:	cmc%inf.rl.ac.uk@nss.cs.ucl.ac.uk
Rutherford Appleton Labs,	UUCP:	..!mcvax!ukc!rlvd!cmc
Didcot, OXON, OX11 0QX, U.K.		cmc@rlvd.uucp
Tel. +44 235 44 6756
FAX. +44 235 44 5831