Topic: Incomplete and continued class definitions
Author: JdeBP@jba.co.uk (Jonathan de Boyne Pollard)
Date: 1995/06/23 Raw View
rraut1@randomc.com wrote:
: The mechanism I propose is to overload the public, protected and
: private keywords. In a class definition, the programmer can add
: a line
: private[10];
: This signals to the compiler that the definition is an incomplete
: definition and that enough room for ten characters should be
: reserved in the class size. I call this construct anonymus member
: data.
Aside from the fact that much of C++ is geared towards the fact that the
programmer *shouldn't* be concerned with the details of object sizes and
alignment requirements, the other objection to your proposal is that you
can get most of what you want by using the language as it stands :
//
// Interface definition
//
class MyClass {
public:
int do_something() ;
MyClass() ;
~MyClass() ;
private:
class Data ;
Data * data ;
} ;
//
// Implementation
//
struct MyClass::Data {
int a ;
float b ;
char * p ;
} ;
MyClass::MyClass () data(new Data) {}
MyClass::~MyClass () { delete data ; }
int
MyClass::do_something()
{
return data->a ;
}
If this is not enough and you want further features like the ability to
upgrade the class API whilst maintaining binary compatibility with
unrecompiled code, then a DirectToSOM C++ compiler will give you that.
Again, no changes to the syntax of the C++ language required.
Author: rraut1@randomc.com
Date: 1995/06/21 Raw View
Although I like many aspects of C++, one of the limitations that
bothers me is not being able to properly encapsulate a class.
C++ currently requires that all the public, protected and private
members of a class be declared in one place. This means that if I
use a type in the private implementation of a class, all clients of
the class must know about the type. If I have two different
implementations (one for a static library, one for a dynamic library)
I have to include the details of both implementations and ifdef on
some preprocessor symbol.
I would like to see incomplete and continued class definitions added
to the language to allow programmers to better encapsulate their
classes.
The reason for having one class definition is simple enough. The
compiler must have certain information available when a class is
being used. 1) The classes the class is derived from. 2) The size
of the class: This is the size of all the data members (public,
protected and private) plus possible space for a vtable pointer.
3) The number of entries in the vtable: This is neccessary so that
derived classes can add their own virtual functions at the end of
the vtable.
Any language addition that allows partial information about a class
must ensure that the compiler has this information before a class
can be used.
The mechanism I propose is to overload the public, protected and
private keywords. In a class definition, the programmer can add
a line
private[10];
This signals to the compiler that the definition is an incomplete
definition and that enough room for ten characters should be
reserved in the class size. I call this construct anonymus member
data.
The protected keyword may be used instead of private with a similar
effect except that the compiler should mark the class as being
unsuitable for subclassing (because the compiler does not have
all the information to properly compile a derived class.)
The first incomplete definition of a class must contain declarations
of all public members. Inline functions may refer to any members in
the current definition but may not use any members declared in
subsequent definitions (psychic compilers not required.)
A class with an incomplete definition may also have a seperate
continued defintion. A continued defintion must have the
same list of base classes as the incomplete declaration. The
first declaration in an incomplete declaration is
public[];
which indicates to the compiler that this definition continues
a previously incomplete definition.
If the compiler sees this line without having previously encounted
an incomplete definition for the class, it should report an error.
If a definition duplicates an incomplete definition but does not
start with this line, it is an illegal duplicate definition.
The members declared in the continued definition will be allocated
from the anonymous member data in the incomplete definition. If the
members overflow the space allocated, the compiler should report
the error. No public members are allowed and protected members are
allowed only if the protected keyword was used to allocate the
anonymous member data. Virtual functions declarations are not
allowed if the incomplete definition did not have any virtual
functions.