Tuesday, 15 February 2011

c++11 - C++ Pre-compiled header and included file organization -



c++11 - C++ Pre-compiled header and included file organization -

i have big native c++ project hundreds of classes each defined in own .cpp , .h file. nil weird here. have of classes declared in pre-compiled header stdafx.h file , included in stdafx.h file shown below (to allow foo reference bar , bar reference foo).

is organization bad practice?

essentially, classes declared , included in every object. assume compiler not going generate overhead binary having things included not necessary compiling particular .cpp object file. of i'm not having problem compile time either. i'm wondering if bad organization code.

i layout because stdafx.h file essenetially code map since classes declared there , reveals nested namespaces easy read format. means individual class .h files don't require several include statements means less code me maintain when making changes filenames or directories.

stdafx.h:

#pragma 1 time namespace myproject { class foo; class bar; } #include "foo.h" #include "bar.h"

foo.h:

#pragma 1 time namespace myproject { class foo { // declare class members, methods, etc } }

foo.cpp:

#include "stdafx.h" namespace myproject { class foo { // define class members, methods, etc } }

in mind there 4 key factors here:

compile time: maintain in mind #include "x.h" embeds code in x.h @ line. if project going grow substantially have care how you're going impact future compile times. foo , bar stability: if foo , bar changing you're going have maintain recompiling stdafx.h circular definitions: no longer able break circular definitions forwards declares. explicit dependencies: won't able @ includes , know used in particular file.

if sense pros outweigh cons listed here, there's nil illegal you're doing go it!

c++ c++11 visual-studio-2013 native

No comments:

Post a Comment