473,626 Members | 3,234 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1474
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**********@y ahoo.com> schrieb im Newsbeitrag
news:%2******** *******@TK2MSFT NGP10.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********@hot mail.com> wrote in message
news:ug******** ******@TK2MSFTN GP10.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.nos pam>
wrote in message news:ub******** ******@tk2msftn gp13.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******** ******@TK2MSFTN GP12.phx.gbl...
"Carl Daniel [VC++ MVP]" <cp************ *************** **@mvps.org.nos pam>
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
2509
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? String formatter = "(whatever)"; Console.Write(formatter,object1,object2, ... ); String formattedString = String.Format(formatter,object1,object2, ... );
4
2296
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? <meta... /> <meta... /> <meta... /> <%= Clinton Gallagher
6
1222
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 anything fancy what-so-ever. Thanks, Basil
2
303
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? String formatter = "(whatever)"; Console.Write(formatter,object1,object2, ... ); String formattedString = String.Format(formatter,object1,object2, ... );
2
2770
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 by an actual timestamp. I noticed that when I use the combination &timestamp under IE 7 the string gets converted to xtamp. This even occurs if I just use document.write('&timestamp'); to write the string out. It results in xtamp! Under a firefox...
0
8269
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8711
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8642
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8368
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7203
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6125
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4094
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2630
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1515
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.