e****9 发帖数: 316 | 1 刚刚电面被击败。
其中的一个题目是
为什么STL的实现都放在.h文件里? | l******c 发帖数: 2555 | 2 C++ template
【在 e****9 的大作中提到】 : 刚刚电面被击败。 : 其中的一个题目是 : 为什么STL的实现都放在.h文件里?
| j**l 发帖数: 2911 | | A***e 发帖数: 130 | 4 My understanding is, the current coding infrastructure, i.e., high-level language source files --> obj/lib files --> executable files, does not support OO, and in particular, does not support template.
Under this infrastructure, if the implementation of STL were put in the .cpp files, which would be compiled to obj/lib files without any information from the users/caller of these STL class/functions, then the obj/lib files had to link to, and work correctly with the users/callers for all possible
【在 e****9 的大作中提到】 : 刚刚电面被击败。 : 其中的一个题目是 : 为什么STL的实现都放在.h文件里?
| a****n 发帖数: 1887 | 5 模板编译有两种方式inclusion model和 separation model, 放在.h 是inclusion
model, separation model 允许把实现放到cpp,不过非常复杂, 所以绝大多数
template 都是放到h文件中。
模板的编译 首先要生成代码,之后才是真正的编译。如果你的程序找不到定义, 那么
也没有办法做第一次生成。 | l********n 发帖数: 54 | 6 我的理解是这样的:
简单的说,这是由于C++的编译方式和模板的特性导致的。
一个例子:test.cpp, template.cpp, template.h
=====================================
template.h
template
class Temp
{
private:
T _val;
public:
void setVal(T& val);
T& getVal();
};
=====================================
=====================================
template.cpp
#include "template.h"
template
void Temp::setVal(T& val)
{
_val = val;
}
template
T& Temp::getVal()
{
return _val; | l******c 发帖数: 2555 | 7 correct,
"C++ does not have binary run-time extensibility, templates can't be linked
and distributed as a library. They must be compiled at compile time, and,
therefore, all implementations of a template algorithm must be included in
the header files in their entirety. You can easily see this by analyzing the
STL standard header files".
【在 l********n 的大作中提到】 : 我的理解是这样的: : 简单的说,这是由于C++的编译方式和模板的特性导致的。 : 一个例子:test.cpp, template.cpp, template.h : ===================================== : template.h : template : class Temp : { : private: : T _val;
|
|