Topic: Class definition split across files?
Author: jamshid@ses.com (Jamshid Afshar)
Date: Mon, 13 Dec 1993 23:14:09 GMT Raw View
Redirectd to c.l.c++.
In article <sp.755233556@canes>, Scott Peters <sp@canes.Rational.COM> wrote:
>russells@ccu1.aukuni.ac.nz (Russell Street) writes:
>>Can you split a class definition across multiple include files?
>
>First off, lets clarify some terms, (or at least speak the same language
>temporarily) There is a class declaration and a class definition.
>The class declaration is obvious. The class definition is c++ code
>defining the actual methods.
Your definition of these terms are contrary to their definitions in
the CPL2, ARM and WP. A class declaration is:
class Foo;
A class definition is:
class Foo {
public:
void f();
void g() {/*...*/}
void h();
};
(though a definition also serves as a declaration)
A member function ("method" if you wish) definition is either the
definition of Foo::g() inside of the Foo definition or the following
definition in the header file containing the Foo definition:
inline void Foo::f() {/*...*/}
or the following definition in a .cc file:
void Foo::h() {/*...*/}
It's *all* C++ code. If you want to dicuss different concepts, please
don't use terms which will confuse regular C++ users. Maybe this
thread should have been named "putting the class implementation in a
different file from the class specification".
>The class declaration may NOT be split among different files.
>The definition of the methods however, can place in many different
>files if you whish. The only got-you's are for in-line methods.
>If you place the method definitions in mulitple files you must make
>sure that the inline function are defined in the header file.
Right. C++ does require showing class implementation details in
header files, but doing otherwise would require radically different
compilation environments than what we have today. If we could do
that, why use C++? Besides, I think people in this thread are going
about it all wrong. Instead of asking how you can write the
specification completely separately from the implementation, why not
ask for tools like browsers which allow you a great deal of
flexibility in what you can view?
I do think it's silly to go through all the hassles of splitting the
definition of inlines and private members into separate files which
get #included in the class definition. Just layout the class nicely
(public first), comment well, define inlines at the end of the header,
etc. and users will be able to get the information they need from your
headers without having to learn the little langauge you create with
the C++ preprocessor.
It's interesting that Eiffel (and I think Sather) take an approach
opposite from what most people in this thread are suggesting. The
programmer writes a class specification and implementation in the same
place. Tools are provided by the Eiffel environment to extract
different views from this one definition. This was done to make the
programmer's job easier, not the compiler writer's.
Jamshid Afshar
jamshid@ses.com
Author: fjh@munta.cs.mu.OZ.AU (Fergus Henderson)
Date: Wed, 8 Dec 1993 13:44:04 GMT Raw View
sp@canes.Rational.COM (Scott Peters) writes:
>russells@ccu1.aukuni.ac.nz (Russell Street) writes:
>
>>[Followups directed to comp.std.c++.]
>
>>Can you split a class definition across multiple include files?
>
>First off, lets clarify some terms, (or at least speak the same language
>temporarily) There is a class declaration and a class definition.
>The class declaration is obvious. The class definition is c++ code
>defining the actual methods.
>
>The class declaration may NOT be split among different files.
This is not correct.
The preprocessor which expands #include directives doesn't know
anything about class declarations. All that the preprocessor knows
about is tokens. If each file contains only complete tokens,
you can split them anywhere you like, you just can't for example put the
start of a comment in one file and then end in another, since that
would be splitting a single token over multiple files.
However, you also need to make sure that the output of the preprocessor
is legal. That means that the declaration of a class must be the
same in every compilation unit in which it is declared.
--
Fergus Henderson fjh@munta.cs.mu.OZ.AU
Author: barmar@think.com (Barry Margolin)
Date: 9 Dec 1993 00:01:37 GMT Raw View
In article <1993Dec6.205124.26063@ccu1.aukuni.ac.nz> r.street@auckland.ac.nz (Russell Street) writes:
>Can you split a class definition across multiple include files?
Sure. Semantic analysis doesn't occur until after preprocessing, which
includes expanding include files.
However, I don't think you can use this feature to do what you seem to be
trying to do.
>defn.h containing:
>
>// -----
>class X {
>public:
> X();
> ~X();
>
>private:
> // The includer of 'defn.h completes the definition
>// -----
>
>and then user.cc containing:
>
>// -----
>#include "defn.h"
>
> int var1, var2;
>};
>
>X::X()
>{
>...
>}
If defn.h is included by any other source files that are linked with
user.o, they also have to complete the declaration compatibly. When you
link together any object files that declare classes with the same names,
they must be identical. See section 3.3 of the ARM.
What is it that you're trying to accomplish with this? It looks like you
hope to hide the private part of the class from other users of the header
file, so that they won't have to recompile if you change it. You can't do
this, as sizeof(X) is specified to be a compile-time constant.
Another possible use for this is for defining a common prefix that might be
used in different programs, which you don't intend to link together. It
seems like it would be better to inherit from X for this, rather than play
games with headers.
--
Barry Margolin
System Manager, Thinking Machines Corp.
barmar@think.com {uunet,harvard}!think!barmar
Author: olsen@verdix.com (David Olsen)
Date: 7 Dec 93 21:47:17 GMT Raw View
In article <sp.755233556@canes> sp@canes.Rational.COM (Scott Peters) writes:
>russells@ccu1.aukuni.ac.nz (Russell Street) writes:
>>Can you split a class definition across multiple include files?
>
>The class declaration may NOT be split among different files.
Yes it can, provided you do it the way the original poster described.
For example:
----------------- foo1.h --------------------
class foo {
public:
foo();
~foo();
void bar(int);
foo *baz();
private:
----------------- foo2.h --------------------
int a, b, c;
};
----------------- foo.cc --------------------
#include "foo1.h"
foo *p1, *p2;
int other_private_member;
#include "foo2.h"
---------------------------------------------
This may be really bad style, but it is legal.
--
David Olsen
olsen@verdix.com
Author: sp@canes.Rational.COM (Scott Peters)
Date: Tue, 7 Dec 1993 03:05:56 GMT Raw View
russells@ccu1.aukuni.ac.nz (Russell Street) writes:
>[Followups directed to comp.std.c++.]
>Can you split a class definition across multiple include files?
First off, lets clarify some terms, (or at least speak the same language
temporarily) There is a class declaration and a class definition.
The class declaration is obvious. The class definition is c++ code
defining the actual methods.
The class declaration may NOT be split among different files.
The definition of the methods however, can place in many different
files if you whish. The only got-you's are for in-line methods.
If you place the method definitions in mulitple files you must make
sure that the inline function are defined in the header file.
--
Scott Peters sp@rational
OO Products
Rational
Author: clint@vsfl.demon.co.uk (Ian Cameron Smith)
Date: Tue, 7 Dec 1993 17:30:54 +0000 Raw View
In article <1993Dec6.205124.26063@ccu1.aukuni.ac.nz> r.street@auckland.ac.nz writes:
> Can you split a class definition across multiple include files?
>
> e.g.,
> defn.h containing:
>
> // -----
> class X {
> public:
> X();
> ~X();
>
> private:
> // The includer of 'defn.h completes the definition
> // -----
>
> and then user.cc containing:
>
> // -----
> #include "defn.h"
>
> int var1, var2;
> };
I assume (quite possibly wrongly) that you're trying to hide
implementation details from your header file. A good aim, IMHO,
but I think your solution won't work.
The programme should compile, because the #include is handled by
the preprocessor, and it doesn't know about C++ syntax. The
compiler only sees the post-included source. However,
unless *every* module that includes defn.h completes the
declaration in *exactly* the same way, you will be using
different declarations of the class in yhe different modules,
and your programme will crash horribly.
Also, IMHO, your solution is messy. That's not a criticissics
bad comment, though, because we are desparately trying to come
up with a way to separate the declaration of a class from its
implementation: a very nasty subject in C++, and we've
considered some pretty messy approaches. My mate waddy has
posted his question/partial solution to comp.lang.c++ under
"Hiding class implementation -solutions anyone ?", qv. Any
help gratefully received at that topic.
--
Ian Cameron Smith clint@vsfl.demon.co.uk
Principal Software Engineer +44 (0)425 474484
Virtual Software Factory LTD
"Go ahead, punk, make my day"
Author: russells@ccu1.aukuni.ac.nz (Russell Street)
Date: Mon, 6 Dec 1993 20:51:24 GMT Raw View
[Followups directed to comp.std.c++.]
Can you split a class definition across multiple include files?
e.g.,
defn.h containing:
// -----
class X {
public:
X();
~X();
private:
// The includer of 'defn.h completes the definition
// -----
and then user.cc containing:
// -----
#include "defn.h"
int var1, var2;
};
X::X()
{
...
}
// ---
etc.
Is this legal?
Russell
---------------------------------------------------------------
Russell Street r.street@auckland.ac.nz
"The world is full of fog and chickens" (try saying it)
-- Spike Milligan