C++ Header and Source file design implementation -


i have few queries regarding design principle of laying out c++ header , source files: have taken on project in previous programmer used have this, particularly annoying because read somewhere shouldn't include .cpp file in .hpp file (the preprocessor copies , pastes .cpp file .hpp)

q1. including .cpp file in .hpp file bad? why?


due problem above, facing many "multiple declaration" errors when load program in eclipse, though added header guards in .hpp files.

q2. should including header guards in .cpp files well? tried later no avail. suggestions on this?

q3. if 2 or more of .cpp files need same header files used best way include header files? should create new header file h1.hpp, include header files need in 2 or more .cpp files , later include in header file in .cpp files(s)?

is efficient approach ?

including .cpp file in .hpp file bad? why?

in typical code setup, yes. serves no useful purpose , can lead "duplicate definition" errors.

more importantly, mixes separation between implementation , interface parts. when file containing implementation meant included, it's changed .inl (from "inline") extension.

should including header guards in .cpp files well?

no. header guards prevent 2 (or more) other headers in 1 translation unit including same header twice. since there's 1 .cpp file per translation unit, problem doesn't occur there.

to illustrate, example inclusion this:

  common.hpp    /      \   /        \ a.hpp     b.hpp   \        /    \      /    file.cpp 

in case, header guard in common.hpp prevents appearing twice in tu introduced file.cpp.

if 2 or more of .cpp files need same header files used best way include header files?

you shouldn't scared long include chain, in general. it's less scary looks. being said, "aggregate" headers can used if headers form tree structure (to make including subsets easier, collections.hpp , collections/vector.hpp + collections/list.hpp) or include every header library.


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -