开发者

What's the best workflow to keep cpp files and header files in sync?

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 syn开发者_运维技巧ched? 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


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.


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?


  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.


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜