class - C++ include vs forward declaration strategy -


consider 2 well-known rules of thumb in c++:

  • use automatic objects wherever possible benefit raii
  • use forward declarations instead of actual includes possible reduce compilation time , number of dependencies

let's assume have following class in c++:

class d {   // methods   a; b b; c c; }; 

following 1st rule of thumb should keep 3 automatic objects , add includes:

#include <a.h> #include <b.h> #include <c.h> class d {   // methods   a; b b; c c; }; 

thus increase compilation time because lot of headers scope when include d.h

following second rule should following:

class a; class b; class c; class d {   // methods   *a; b *b; c *c; }; 

but in case have manage creation/deletion of objects myself, know leads errors , memory leaks.

is there solution problem? using private implementation classes pain i'd avoid if possible.

(i assume you're strictly talking case d owns child object instances) seems subjective, opinion-based answer: use automatic objects , if corresponding headers big compilation times noticeably longer, fix that problem.


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

jquery - javascript onscroll fade same class but with different div -