Newsgroups: comp.lang.c++
Path: utzoo!henry
From: henry@utzoo.uucp (Henry Spencer)
Subject: Re: Can we hide the private part of a class ?
Message-ID: <1988Sep29.044111.16104@utzoo.uucp>
Organization: U of Toronto Zoology
References: <1358@stratus>
Date: Thu, 29 Sep 88 04:41:11 GMT

In article <1358@stratus> hsu@stratus (Yung-Kao Hsu) writes:
>This features make package/module interfaces cleaner (you don't see any
>implementation information at all), and there are more advantages.
>But, I couldn't find anything similiar in C++ !!

The reason is that these features hurt code efficiency.  The user does
not, in any case, get much benefit from seeing those implementation
details, because he can't make much use of them... but the compiler
can and does.  Consider your example:

>	class X { int x[10]; \\ private decalarations
>		  public:
>		  W,X, ...
>		};

The user can't access x, so seeing it won't do him much good.  However,
the compiler now knows exactly how big a variable of class X is, so it
can generate code that knows this.  If those private declarations were
hidden somewhere else, either the compiler has to get much more complex
or else class variables can't be as efficient as ordinary variables.
For example, as I recall (it's been a while...), since Modula 2 doesn't
tell the compiler how big the variable is, all the compiler can do for
space allocation is to allocate a pointer to the variable, and do all
accesses via that pointer.  This can be pretty slow.  The advantages
of the C++ method are even more prominent when you have something like:

	class foo {
		int x;
	public:
		int twice() { return 2*x; }
	} myvariable;

In C++, the code that results from using "twice" is essentially
"2 * myvariable.x"; note that there is no function call needed!  In
Modula 2, you can't do this without the full function-call overhead.

Remember, C++ is aimed primarily at the C community, which has a long
tradition of caring A LOT about efficiency.  There are many, many people
who are willing to adopt C++ only because they can be fairly sure that
they aren't losing significant efficiency by doing so.  C++, like C,
specializes in being fast rather than pretty.
-- 
The meek can have the Earth;    |    Henry Spencer at U of Toronto Zoology
the rest of us have other plans.|uunet!attcan!utzoo!henry henry@zoo.toronto.edu