- 论坛徽章:
- 2
|
回复 6# cjog
C++的inline函数或者函数模板都可以放在头文件中, 被多个翻译单元包含并编译。
最终链接时不会重复定义。
member function放在定义体内也是inline函数。
下面的都可以放在头文件中
- inline int abs(int x) { return x<0? -x: x; }
- // inline函数
- template<typename T>
- T max(T a, T b) { return a<b? b: a; }
- // 函数模板
- template<typename T>
- inline T min(T a, T b) { return a<b? a: b; }
- // inline函数模板
- struct point
- {
- double radius() const { ... }
- // inline 函数
- ...
- };
- template<typename T>
- class vector {
- size_t size() const { ... }
- // inline函数模板
- void push_back(T const& x);
- };
- template<typename T>
- void vector<T>::push_back(T const& x) { ... };
- // 函数模板
复制代码 |
|