I would like to understand the pros/cons of forward declaration for classes/structs that are in different files in header only development. I understand that using forward declaration in normal code-development (implementation in .cpp files), but what does it actually bring in header only development?
Asked
Active
Viewed 226 times
0
-
Take a look at the particular example from the standard library: [What is the `
` header?](https://stackoverflow.com/questions/4300696/what-is-the-iosfwd-header) – Evg Nov 14 '19 at 10:19 -
possible duplicates https://stackoverflow.com/questions/553682/when-can-i-use-a-forward-declaration – VJAYSLN Nov 14 '19 at 10:19
-
For this project I am developing in c++ with cuda and in order to reduce the building time want to have only one .cu file. So basically all classes have one header file .hpp and the implementation is in .cuh (readability seperation) – nionios Nov 14 '19 at 10:20
1 Answers
2
The pros and cons are same as with non header only programming. If class b depends on definition of class a, but class a depends on the declaration of class b, then it is a good practice to forward declare class b before definition of class a because otherwise the program would be ill-formed. (Technically you could forward declare within definition of a by using an elaborated type specifier, but some people don't like that for stylistic reasons).

eerorika
- 232,697
- 12
- 197
- 326