Should my code implementation go in my .CPP or the .H header file?

B

Bruce

I have seen sample code that is implemented in the .h header file ("java
style") and I have seen samples where the code was implemented in the
..CPP file. Which is correct? What is the best place to put my code
implementation in VC++ DotNet?

P.S. The code is for a control where I am only going to distribute the
DotNet DLL.

Bruce
 
B

Bruno van Dooren [MVP VC++]

I have seen sample code that is implemented in the .h header file ("java
style") and I have seen samples where the code was implemented in the .CPP
file. Which is correct? What is the best place to put my code
implementation in VC++ DotNet?

P.S. The code is for a control where I am only going to distribute the
DotNet DLL.

Whenever possible, your code implementation should be in the cpp file, and
the declaration in the h file.
exceptions:
- template classes should go into the h file completely because there is no
other option.
- IDE generated classes like Window Forms classes. These are implemented in
h files because the wizard does not understand the concept of difference
between declaration and implementation.

The reasons for the split is that a change in the h file causes a
recompilation in all the cpp files that include it. you want to prevent as
much as possible.
It is also the worldwide standard to program this way,
And finally, If you build libraries you have to have a separate header file
with the declarations anyway.

--

Kind regards,
Bruno van Dooren
(e-mail address removed)
Remove only "_nos_pam"
 
B

Bruce

Bruno said:
The reasons for the split is that a change in the h file causes a
recompilation in all the cpp files that include it. you want to prevent as
much as possible.
It is also the worldwide standard to program this way,
And finally, If you build libraries you have to have a separate header file
with the declarations anyway.

Thank you very much for the clarification. I have been programming in
c++ for years but fairly new to DotNet. I knew the reasons to use CPP
files. I just thought I had missed something new after seeing so many
DotNet samples in the header file.

Bruce
 

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