By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,962 Members | 1,767 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,962 IT Pros & Developers. It's quick & easy.

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

P: n/a
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.
Mar 20 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Abubakar wrote:
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
Mar 20 '06 #2

P: n/a
Abubakar wrote:
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
Mar 20 '06 #3

P: n/a
Dear Abubakar,
there is a tool which might help you to move definitions into cpp now.
http://blogs.msdn.com/devdev/archive...19/453891.aspx
"Abubakar" <em**********@yahoo.com> schrieb im Newsbeitrag
news:%2***************@TK2MSFTNGP10.phx.gbl...
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.

Mar 20 '06 #4

P: n/a
> 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.
"Tom Widmer [VC++ MVP]" <to********@hotmail.com> wrote in message
news:ug**************@TK2MSFTNGP10.phx.gbl... Abubakar wrote:
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

Mar 21 '06 #5

P: n/a
gc
"Carl Daniel [VC++ MVP]" <cp*****************************@mvps.org.nospam >
wrote in message news:ub**************@tk2msftngp13.phx.gbl...
Abubakar wrote:
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

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?
Mar 24 '06 #6

P: n/a
"gc" <ga****@nospamm.unidial.com> wrote in message
news:OB**************@TK2MSFTNGP12.phx.gbl...
"Carl Daniel [VC++ MVP]" <cp*****************************@mvps.org.nospam >
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
Mar 25 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.