CC++项目中.h和.inc文件区别

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

CC++项⽬中.h和.inc⽂件区别
原问题:
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.
英⽂原版回答
.inc files are often associated with templated classes and functions.
Standard classes and functions are declared with a .h file and then defined with a .cpp file. 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 .h file.
To keep the declaration and definition separate, the definition is placed in its own file and included at the end of the .h file. 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_H
Definition example:
// Foo.inc
#include "Foo.h"
template<typename T>
Foo<T>::Foo() {
// ...
}
template<typename T>
void Foo<T>::DoSomething(T x) {
// ...
}。

相关文档
最新文档