tags:

views:

79

answers:

5

Hi!

I'm trying to learn C++ for Qt development, and I'm a little scared of header files. What I'd like to know is, what's the best workflow for keeping *.cpp and *.h files synched? For example, is the norm to write the class file and then copy the relevant info over to the header?

Sorry if this doesn't make any sense...I'm just looking for an efficient workflow for this.

Thanks!

  • Justin
+3  A: 

So:

  • You'll get linker errors if something's in the header, used by a client, and not in the cpp.
  • You'll get compile errors if something in the cpp is not defined or is defined differently in the .h

What scenario are you worried about?

David Gladfelter
Thanks for the quick reply David. To clarify, I'm really only trying to figure out if people write the cpp file first, the header file first, or if they go back and forth. Is there any convention on this to make it less error prone?
jgoney
@jgoney: Where is the error going to **come** from? The instant you break the rules the compiler will blow up in your face!
Billy ONeal
That said, is it actually a bad idea to both declare and define in the header file?
fingerprint211b
@jgoney, when writing a class from scratch I generally write out a header file and then paste the whole thing into the .cpp file. I then start working on the method bodies and having the instance variables in front of me helps with that. If I was better about lining up multiple documents in multiple tiled windows, I wouldn't need to do that, of course.
David Gladfelter
Thanks David....that's the kind of thing I was trying to figure out! Sounds like a good way to work!
jgoney
A: 

You keep them both open at the same time (I frequently use a horizontal split for this) and when you change one file you change the other file. It's just the same as how you keep the prototypes at the top of a file in sync with the function definitions at the bottom of the file when you're writing a program that fits in a single .cpp file.

Ken Bloom
If I only had a display big enough for this...
Billy ONeal
Good idea on the split view! I think that should be doable in most IDEs.
jgoney
+3  A: 

For example, is the norm to write the class file and then copy the relevant info over to the header?

While there is no single standard approach, its usually a good idea to:

  • first think about the public interface
  • put that in the header
  • implement in the source file accordingly
  • update the header if needed

Jumping straight into the implementation can make for a painful refactoring later on.

Georg Fritzsche
...and refactoring is exactly the kind of thing I'm trying to avoid! In fact, I think the original question came to my mind because shifting between the .h and .cpp files reminded me of a discussion I had with a professor once (when I was learning to program) about avoiding spaghetti code in FORTRAN. This seems somehow similar...
jgoney
@jgoney: I don't really see what it has to do with the header files per se.
Ken Bloom
@Ken: Shuffling back and forth between a header file and a cpp file reminds me of the up and down you have to do when you're looking over someone's bad FORTRAN code full of a bunch of GOTOs. It seems like working back and forth that way could lead to the same type of disorganized, error-prone code. Not the best example (anachronistic, if nothing else), but I'm probably not the best programmer to be dissecting this either.
jgoney
+1  A: 
  1. Create the test code that uses your class/functions (this way you can play with the end results before even writing any code)
  2. Create the header with the class and the methods.
  3. Implement those methods in your cpp file

If you run into the case where you have to change the signature of the methods, you will need to manually change it both place (header + source).

You can shell out a small amount of money and buy Visual Assist X. With Visual Assist X, you right click the method in your cpp file, choose Refactor -> Change Signature. Perform the change and hit Ok. It is changed in both places.

So, in short, there is no way to keep them in sync automatically, but with the right refactor tool, your life will be better.

Dat Chu
+1  A: 

In general for the first version I write the class file in the .h and when done I copy the method declarations to the .cpp and there change them into definitions (i.e. methods and their bodies). In this phase I declared only the public methods of the class, because I'm more concerned about its interface than the internals.

Later, when I'm implementing the public methods, if I need a new private method I begin by calling it wherever it's needed, so I get the parameters well defined. Then I write its declaration in the .h and go back to the .cpp to write its body.

It may be of interest to you the concepts of Tracer Bullet Design, which fits well with this workflow. It's described in the book The Pragmatic Programmer:

http://www.amazon.com/Pragmatic-Programmer-Journeyman-Master/dp/020161622X/ref=sr_1_1?ie=UTF8&s=books&qid=1279150854&sr=8-1

This page contains a brief description http://www.artima.com/intv/tracer2.html

Fabio Ceconello
Thanks man....that book looks like just the thing a beginner like me needs to read!
jgoney