Include header in implementation

Suppose I have a header foo.hlike this:

#ifndef FOO_H
#define FOO_H
#include <string>
#include "non_standard_class.h"

std::string foo(MyClass a);
...
#endif

and the implementation foo.cppwill be

#include <vector>

#include "foo.h"

std::string foo(MyClass a)
{
   std::vector<int> x;
   MyClass b;
   ...
}

Is there a good opportunity to re-enable <string>and non_standard_class.hin foo.cpp? The thing is, if I read foo.cpp, how can I understand where MyClass came from? I need to see foo.h, but it will be harder.

+5
source share
3 answers

Practice that I follow:

  • foo.h should directly include all headers in its source code (i.e. #include <string>, if there is one in foo.h std::string)
  • foo.cpp foo.h , , , foo.h

: foo. * , . foo.cpp( foo.h), foo.cpp, foo.h.

, #include <string> foo.cpp, foo.h.

, string #include <vector>, foo.h std::vector. #include <vector> foo.h, โ€“ , string .

+7

() : , .

, , /, , , , , 8 ( ) ( 1 , ), . , , , , .

, . , - , , ( , , ).

, ( ), .

+2

, , foo.cpp foo.h, . , .h, , .

+1
source

All Articles