473,320 Members | 1,887 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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

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
6 1448
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
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
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
> 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
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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Unemployed VC++ guy | last post by:
It seems that there is basically no difference between the Console.Write() & String.Format(), other than the output being sent to the console output stream instead of a string. Is this accurate? ...
4
by: clintonG | last post by:
Anybody know how to dynamically write the meta tags using code so they are formatted on a separate line in the HTML source? Preferred or optimal framework classes that may be used in this regard? ...
6
by: Basil Fawlty | last post by:
Hi everyone, I have VB.NET 2003 SE, I understand that I can write and compile C and C++ code in this tool. Is that right? If so, how would I go about doing something like that? It won't be...
2
by: Unemployed VC++ guy | last post by:
It seems that there is basically no difference between the Console.Write() & String.Format(), other than the output being sent to the console output stream instead of a string. Is this accurate? ...
2
by: bruceturek | last post by:
I'm having an issue that I'm sure there is a simple fix for! I'm creating a java script that will dynamically create a URL. In the process I need to include a URL parameter '&timestamp=' followed...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.