C ++: including class definition in header file

The number of messages is rather inappropriate, that the source code should not go in the header, and the header files should be minimized. I stuck to this with my own code, but I want to use someone else's code to achieve a specific goal (the code here is http://ftp.arl.mil/random/ ).

I notice that this is basically one gigantic header file that defines the class. Can this be left in the header file? Should I copy all of this into a .cpp file and create a new .h that just declares functions, structures, etc.?

If I split it into .cpp and .h, as I suggest, will this work? Or should classes be in a header that all source code should access?

+7
c ++ class header
source share
3 answers

Declarations (indicating that something exists) that should be seen in more than one cpp file should go into the header files. Declarations that are local to a single cpp file must be in the cpp file itself.

Definitions (providing body functions or variable allocation / initialization) should usually be included in cpp files, but not always.

The question you need to understand is whether the compiler has enough information to do its job if it saw a header file and not the corresponding cpp file.

For example: you can call a method if the compiler saw the declaration (prototype of the method) - if only this method is not general (the template method or a member of the template class) or built-in, in this case the compiler is needed to see the definition (method body) too.

Therefore, the usual methods go to cpp files; template methods go to header files; built-in methods go to header files (etc.).

There are other situations in which definitions apply to header files, including static member constants. All this returns to providing the compiler with the information he needs with one hand, and minimizing the connection between the individual compilation units on the other. Again there are no hard and fast rules, just guidelines combined with the knowledge and experience of the developer who wrote the code.

+6
source share
Files

.h are usually shared between many .cpp files. Global variables and function code should not be in the header files because they will duplicate during binding.

Constants, defines function headers and class declarations are great in header files. You do not need to declare the same thing several times, and you can share definitions between .cpp files.

+5
source share

source code should not be in the header, and header files should be kept to a minimum.

This is a popular statement, and although this may not be bad advice, you should not draw absolute conclusions from it. Sometimes headings should be minimal and not include definitions. Sometimes the opposite is true. There are reasons why you will do one or the other, but β€œpeople say” are not one of them.

Consider the standard C ++ library. Better yet, think about Boost. Some prestigious C ++ experts have stated that Boost is the most well-designed C ++ library in history. But if you look at the libraries, you will see that they are basically just giant header files for the most part. How does this fit with what they say?

The fact is that you must understand the reasons why certain files are created as they are, and make up your own mind about what is right and wrong for each situation.

Should I copy all of this into a .cpp file and create a new .h that just declares functions, structures, etc.

I would say probably not. It sounds like a nightmare recipe for service to me. This would be my first instinct to use a third-party library as the author of the library suggested. Thus, you will not find yourself outside the support grid, and you will not introduce a new set of complications that you will be fully prepared to figure out on your own. Unless you have a specific, demonstrable reason to change the architecture of the library, do not. "They say that is not a good reason for me.

+4
source share

All Articles