原問題:Difference between .h files and .inc files in c
C/C++的標准慣例是將class、function的聲明信息寫在.h文件中。.c文件寫class實現、function實現、變量定義等等。然而對於template來說,它既不是class也不是function,而是可以生成一組class或function的東西。編譯器(compiler)為了給template生成代碼,他需要看到聲明(declaration )和定義(definition ),因此他們必須不被包含在.h里面。
為了使聲明、定義分隔開,定義瀉在自己文件內部,即.inc文件,然后在.h文件的末尾包含進來。當然除了.inc的形式,還可能有許多其他的寫法.inc, .imp, .impl, .tpp, etc.
英文原版回答
.incfiles are often associated with templated classes and functions.Standard classes and functions are declared with a
.hfile and then defined with a.cppfile. However, a template is neither a class nor a function but a pattern that is used to generate a family of classes or functions. In order for the compiler to generate the code for the template, it needs to see both the declaration and definition and therefore they both must be included in the.hfile.To keep the declaration and definition separate, the definition is placed in its own file and included at the end of the
.hfile. This file will have one of many possible file extensions.inc,.imp,.impl,.tpp, etc.Declaration example:
// Foo.h #ifndef FOO_H #define FOO_H template<typename T> class Foo { public: Foo(); void DoSomething(T x); private: T x; }; #include "Foo.inc" #endif // FOO_HDefinition example:
// Foo.inc #include "Foo.h" template<typename T> Foo<T>::Foo() { // ... } template<typename T> void Foo<T>::DoSomething(T x) { // ... }
