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

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

原问题: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.

英文原版回答

.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

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

Foo::Foo() {

// ...

}

template

void Foo::DoSomething(T x) {

// ...

}

相关推荐

《dnf》新版本材料作用介绍
365bet足球实时动画

《dnf》新版本材料作用介绍

📅 07-17 👁️ 7169
每个月份啥花开?什么地方有花开?大庆这份“赏花时间表”和“赏花地图”快收好
营销洞察 | 2022卡塔尔世界杯专题分析报告
mobile365bet365com

营销洞察 | 2022卡塔尔世界杯专题分析报告

📅 07-02 👁️ 6538
男篮世界杯伊朗对阵突尼斯:谁能笑到最后?
mobile365bet365com

男篮世界杯伊朗对阵突尼斯:谁能笑到最后?

📅 07-07 👁️ 4347
vivo iQOO Z3屏幕规格和显示效果如何
爱彩365彩票官方app下载

vivo iQOO Z3屏幕规格和显示效果如何

📅 07-13 👁️ 3793
🎫花千骨兑换码
mobile365bet365com

🎫花千骨兑换码

📅 06-28 👁️ 3898
璐字取名寓意和含义
365bet足球实时动画

璐字取名寓意和含义

📅 07-04 👁️ 3014
溏心风暴3阿九最后黑化了吗 凌乘风黑化原因揭晓
365bet足球实时动画

溏心风暴3阿九最后黑化了吗 凌乘风黑化原因揭晓

📅 07-09 👁️ 7574
小米4s参数配置详情
爱彩365彩票官方app下载

小米4s参数配置详情

📅 07-06 👁️ 9966