[background=rgb(250, 251, 252)]Hi everybody,[/background]
[/font][color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif]
[background=rgb(250, 251, 252)]I want to discuss a topic discussed in a previous thread: [/background]
[/font]http://www.gamedev.net/topic/510239-separate-header-files-for-declarations-and-definitions/ : [color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif][background=rgb(250, 251, 252)] declaration and definition of classes in separate header files.[/background]
[/font][color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif]
[background=rgb(250, 251, 252)]I have been reading the Scott Meyers's Effective C++ book (3ed edition):[font=courier new,courier,monospace][/font][/background]
[/font][font=courier new,courier,monospace]Item31: Minimize compilation dependencies between files [/font][color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif][background=rgb(250, 251, 252)]but there are some points that are not so clear..[/background]
[/font][color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif]
[background=rgb(250, 251, 252)]I copy and past the same question of the old thread because it's what I wanted to ask:[/background]
[/font][color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif]
[background=rgb(250, 251, 252)]"I often use forward declaration of classes in my header files in order to avoid including unnecessary class headers and thus increasing dependencies (and therefore compile times). E.g.[/background]
[/font][color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif]
[background=rgb(250, 251, 252)]
[color=blue][font=CourierNew]class[/font] A; [color=gray][font=CourierNew]// Avoids having to include "A.h"[/font]
[color=blue][font=CourierNew]class[/font] B {
[color=blue][font=CourierNew]public[/font]:
SomeFunction(A* parameter); [color=gray][font=CourierNew]// Compiler is happy, because A has been declared[/font]
};
[/background]
[color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif]
[background=rgb(250, 251, 252)]However, Scott Meyers [/background]
[/font]Effective C++[color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif][background=rgb(250, 251, 252)] (3rd edition, page 144) suggests that it is best practise to actually put these forward declarations into a separate header file and include that instead of the forward declaration. For instance, in the above example class A would be split into 3 files: * A.h - The definitions * A.cpp - The implementations * A_fwd.h - The forward declarations (but no definitions or implementations) A_fwd.h would be included in the above code, rather than explicitly declaring class A. My query is though, is this really beneficial and do people really follow this practise? The justification for this best practise seems to be that if a class declaration changes then you only have to change it in one place (the declaration header). This doesn't see to make sense though for two reasons: * Why would my class declaration change? My definitions and implementations may but the forward declaration simply states that a class of that name exists. If I decided to rename a class then ok, but... * Even if I did change the declaration, I still have to visit all my class definitions & implementations and rename the class everywhere it's used! (This could be avoided if I'm using pimpl unless it's the interface class that's changing, but I don't think we need to get into that). Any thoughts? Cheers! "[/background]
[/font]