c++ - Include in header file vs. forward-declare and include in .cpp -


i have class b, , want call members form class a. so:

1.

//a.h     class b;  class  {  private:     b* m_p;  };   //a.cpp #include "b.h" 

2.

// a.h #include "b.h"  class  {  private:      b * impl_;  };  

which way better , 2 similar when small project not dependence evolves?

your first way of doing means in a.h, existence of class b known, not definition. limits can b inside a.h. example, can have variables of type b *, not variables of type b (because declaration of variable of type b compiler must able see full definition of b). also, if have variables of type b *, can't dereference pointer (because that, too, definition of b must known).

therefore, second choice – doesn't have these problems – preferred, , people use of time.

it's special cases in first method may useful. example:

  • if .h files include each other (but may number of further problems, regarding include-guards; difficult , avoided);
  • if b.h extremely large , complex, you'd avoid including wherever possible because slows down compilation process.

Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -