Where to declare structures, etc.?

Should all structures and classes in the header file be declared? If I declare a struct / class in the source file, what do I need to put in the header file so that it can be used in other files? Also, are there any resources that show some standard C ++ methods there?

+6
c ++ coding-style
source share
2 answers

Should all structures and classes in the header file be declared?
Yes. EDIT: But their implementation should be in cpp files. Sometimes users from C # or Java do not realize that a C ++ implementation can be completely separate from the class declaration.

If I declare a struct / class in the source file, what do I need to put in the header file so that it can be used in other files?
You can not. The compiler needs a complete declaration of the class available in any translation unit that uses this class.

Also, are there any resources that show some standard C ++ working methods?
You can simply download the source for any number of open source applications. Although the only fully compatible thing you are likely to see is using heading protection and saving all ads in header files.

+5
source share

The whole point of header files is to declare interfaces intended for sharing in other source files. Often people declare abstract types in header files and implement them in source files as needed. This means, of course, that the newly implemented type will be available only for this source file. If you need to use a type for multiple files (which usually happens), you will need to use header files.

C ++ faq is usually a great resource for best practices.

+2
source share

All Articles