should I write code in ( *.h && *.cpp ) || only *.h

A

Abubakar

Hi,
I'm working on a project in unmanaged c++. I was writing all (most of) my
code in header files, that is, no seperation of code in header and cpp
files, as usually is done in c++. I feel pretty comfortable doing this as it
my first time writing code in c++. My background is in C#. Now one senior
guy ( a non-developer ), came up and wanted to see my code, and he said what
I was doing is wrong. What I should do is declare all my code interface
stuff (function prototypes for classes) in header files, than create a cpp
file in which include the header file and provide definitions of those
delarations.
Although I have never worked on c++ projects before, but I have read a lot
of c++ code before and I know that what the senior non-dev guy is saying is
right. But whats wrong with writing all the code in header files (is it
called inlining?)? What difference does it make? Any performance problems? I
think "of course not". I just feel pretty ok dealing only with header files.

Plz advice.

Regards,

Abubakar.
 
T

Tom Widmer [VC++ MVP]

Abubakar said:
Hi,
I'm working on a project in unmanaged c++. I was writing all (most of) my
code in header files, that is, no seperation of code in header and cpp
files, as usually is done in c++. I feel pretty comfortable doing this as it
my first time writing code in c++. My background is in C#. Now one senior
guy ( a non-developer ), came up and wanted to see my code, and he said what
I was doing is wrong. What I should do is declare all my code interface
stuff (function prototypes for classes) in header files, than create a cpp
file in which include the header file and provide definitions of those
delarations.
Although I have never worked on c++ projects before, but I have read a lot
of c++ code before and I know that what the senior non-dev guy is saying is
right. But whats wrong with writing all the code in header files (is it
called inlining?)? What difference does it make? Any performance problems? I
think "of course not". I just feel pretty ok dealing only with header files.

There are several problems with your approach:
- There's nothing to compile! You can't compile a header file. So lets
assume you have exactly one .cpp file for your whole program.
- If one header includes another header, it will implicitly include
everything that the implementation of the other header relies on. This
means that any change you make in any part of your whole program will
mean you have to recompile the entire program - not a problem for small
programs, but for medium and large programs a full recompilation can
take hours.
- A large program becomes impossible, since it requires too much memory
to compile the program as a single .cpp file.
- Every function is implicitly (or explicitly) inline, which is likely
to lead to major code bloat.
- Circular link-time dependencies are impossible without separate .h and
..cpp files, not that this is a particularly bad thing.
- More things I can't think of.

The separation of .h and .cpp files isn't just for fun. Why did you
think it was?

Tom
 
C

Carl Daniel [VC++ MVP]

Abubakar said:
Hi,
I'm working on a project in unmanaged c++. I was writing all (most
of) my code in header files, that is, no seperation of code in header
and cpp files, as usually is done in c++. I feel pretty comfortable
doing this as it my first time writing code in c++. My background is
in C#. Now one senior guy ( a non-developer ), came up and wanted to
see my code, and he said what I was doing is wrong. What I should do
is declare all my code interface stuff (function prototypes for
classes) in header files, than create a cpp file in which include the
header file and provide definitions of those delarations.
Although I have never worked on c++ projects before, but I have read
a lot of c++ code before and I know that what the senior non-dev guy
is saying is right. But whats wrong with writing all the code in
header files (is it called inlining?)? What difference does it make?
Any performance problems? I think "of course not". I just feel pretty
ok dealing only with header files.

A quick addition to Tom's comments -

C++ and C# are languages that come from different eras. The C++ language
specification was crafted very carefully to allow a one-pass compiler to be
written. Such compilers pass over the source a single time and (at least
ideally) never build or maintain a fully parsed representation of the entire
program.

C#, on the other hand, was designed with modern compiler practice in mind -
the compiler does in fact build a fully parsed representation of the entire
program and can make multiple passes over that parsed form to resolve
references, etc.

That's why, for example, in C++ you have to declare forward references,
while in C# you don't. In C++ you have to explicitly include the
declarations of objects/types in another module of the program, while in C#
you don't.

As a result of the one-pass, multiple compiland model for C++ compilation,
simply putting all the code in the header files may make C++ look like C#,
but it's rarely a good solution. You'll still need forward declarations,
and you'll have to explicitly include declarations for a module into any
other module that references it. As a result, you'll naturally drive your
code towards one of two configurations: 1. Everything is inline. 2.
Separation of interface from implementation. While option 1 may look like
C#, under the C++ compilation model it's quite a lot different and, as Tom
pointed out, doesn't scale well. Option 2 is- well, exactly how C++ has
been traditionally written, and for good reason - it's the pattern that fits
the compilation model.

There are some in the C++ compiler community that suggest that the one-pass,
multi-module, compile-link build model of C++ has been stretched about as
far as it can go and if C++ is to continue to compete as a language that we
need a new C++(++?) that uses a more C#-like compilation model. Time will
tell...

-cd
 
A

Abubakar

The separation of .h and .cpp files isn't just for fun. Why did you
think it was?

I wasnt doing it for fun, its just that its my first time working on a c++
project and I just did what I felt comfortable with.

I think I get the point, should maintain the headers.

Anyway, thanks for nice answers Tom, Carl, n Tanja.

regards,

Ab.
 
G

gc

Carl Daniel said:
A quick addition to Tom's comments -

C++ and C# are languages that come from different eras. The C++ language
specification was crafted very carefully to allow a one-pass compiler to
be written. Such compilers pass over the source a single time and (at
least ideally) never build or maintain a fully parsed representation of
the entire program.

C#, on the other hand, was designed with modern compiler practice in
mind - the compiler does in fact build a fully parsed representation of
the entire program and can make multiple passes over that parsed form to
resolve references, etc.

That's why, for example, in C++ you have to declare forward references,
while in C# you don't. In C++ you have to explicitly include the
declarations of objects/types in another module of the program, while in
C# you don't.

As a result of the one-pass, multiple compiland model for C++ compilation,
simply putting all the code in the header files may make C++ look like C#,
but it's rarely a good solution. You'll still need forward declarations,
and you'll have to explicitly include declarations for a module into any
other module that references it. As a result, you'll naturally drive your
code towards one of two configurations: 1. Everything is inline. 2.
Separation of interface from implementation. While option 1 may look like
C#, under the C++ compilation model it's quite a lot different and, as Tom
pointed out, doesn't scale well. Option 2 is- well, exactly how C++ has
been traditionally written, and for good reason - it's the pattern that
fits the compilation model.

There are some in the C++ compiler community that suggest that the
one-pass, multi-module, compile-link build model of C++ has been stretched
about as far as it can go and if C++ is to continue to compete as a
language that we need a new C++(++?) that uses a more C#-like compilation
model. Time will tell...

-cd
I just played with my new VS.NET 2005. It seems that all the source code
generated from win form is put into .h file. Does this mean that MS VS.NET
has different type of compiler which is similar to their C# compiler?
 
C

Carl Daniel [VC++ MVP]

gc said:
"Carl Daniel [VC++ MVP]" <[email protected]>
I just played with my new VS.NET 2005. It seems that all the source code
generated from win form is put into .h file. Does this mean that MS VS.NET
has different type of compiler which is similar to their C# compiler?

No, it just means that the forms designer only knows how to work with 1
file - the header file. That's not an endorsement that it's really good
C++ coding style, it's just the way it is.

-cd
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top