Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / March 2006

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Abubakar - 20 Mar 2006 15:58 GMT
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.
Tom Widmer [VC++ MVP] - 20 Mar 2006 16:50 GMT
> Hi,
> I'm working on a project in unmanaged c++. I was writing all (most of) my
[quoted text clipped - 11 lines]
> 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
Abubakar - 21 Mar 2006 06:15 GMT
> 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.

> > Hi,
> > I'm working on a project in unmanaged c++. I was writing all (most of) my
[quoted text clipped - 33 lines]
>
> Tom
Carl Daniel [VC++ MVP] - 20 Mar 2006 17:11 GMT
> Hi,
> I'm working on a project in unmanaged c++. I was writing all (most
[quoted text clipped - 12 lines]
> 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
gc - 24 Mar 2006 22:56 GMT
>> Hi,
>> I'm working on a project in unmanaged c++. I was writing all (most
[quoted text clipped - 50 lines]
>
> -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?
Carl Daniel [VC++ MVP] - 25 Mar 2006 02:21 GMT
> "Carl Daniel [VC++ MVP]" <cpdaniel_remove_this_and_nospam@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
Tanja Krammer - 20 Mar 2006 17:23 GMT
Dear Abubakar,
there is a tool which might help you to move definitions into cpp now.
http://blogs.msdn.com/devdev/archive/2005/08/19/453891.aspx

> Hi,
> I'm working on a project in unmanaged c++. I was writing all (most of) my
[quoted text clipped - 22 lines]
>
> Abubakar.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.