2

When my project is really has great code structure, where using almost everywhere different datatypes, whether it makes sense to store many types (mostly static, eg structs) of data in a single header?

MyGameDataTypes.h for example. Where I declared the FCat, FDog, FElephant, etc...

 struct FCat
 {
      ...  // much properties here
 };

 struct FDog
 {
      ...  // much properties here
 };

 struct FElephant
 {
      ...  // much properties here
 };

 /// AND MUCH MORE

Or for best way I must create every header individually for each my struct?

Cat.h where struct FCat

Dog.h where struct FDog

Elephant.h where struct FElephant

etc...

Or there are better ways and this above is nonsense?

Artem Selivanov
  • 1,867
  • 1
  • 27
  • 45

1 Answers1

1

You can write several structures in the same .h file, and have only one file for all your animals structure. For example, a animals.h file could contain FCat FDog and FElephant. The most important thing is to have a coherence in your code. Having all your animals structure in one place is coherent, having a animal, a car and a building structure in the same file may not be.

Maybe it is already the case, but you should protect your header files from the multiple inclusion like that.

Community
  • 1
  • 1
Olivier
  • 118
  • 11
  • Headers is protected from multiple inclusion in every file with `#pragma once` directive. Okay, this is animals, but if all data structures has no explicit category? What I must do in this way? Besides, new data types appear frequently in code. – Artem Selivanov May 23 '16 at 12:50
  • 1
    There should be a link between your data structures. If this is not what they represent, it could be how they are used. If you are using `struct A` and `struct B` always together for example. Or if `struct A`, `struct B` and `struct C` are a part of `struct D`, they could be in the same file – Olivier May 23 '16 at 13:00
  • I think thats right, but it's question about global code organization. Are there any reasons to store almost data types in one header as "Library of types"? Or find a way to stuff all data types closer to the business logic? Or something else? I think everything has its place, and types must placed in type library, but perhaps it is wrong and has pitfalls. For example, full project recompiling. But also perhaps it can also be neglected. – Artem Selivanov May 23 '16 at 13:13
  • I guess every developer has his own preference, but I would privilege to group data structures depending on the program's logic, like I said previously. That way, you (and the other developers who look at your code) can easily understand the program's structure and access the file you need quickly. I think it's important to have logical headers file to avoid to include data structures you won't use in your source file. – Olivier May 23 '16 at 13:32