来自 google v8 js 引擎的代码,allocation.h:
template <typename T>
static void DeleteArray(T* array) {
delete[] array;
}
这是一个函数模板(顶级,不在任何类中)。 但是 static 关键字有什么用呢?
最佳答案
它是一个模板实际上是一个附带问题。
它是静态的意味着该函数在文件外部不可见(但由于它位于 header 中,并且 header 实际上是包含它们的文件的一部分,这意味着包含 header 的文件外部;并且每个文件包含 header 实际上有其自己的、相同但私有(private)的函数版本)。
来自http://msdn.microsoft.com/en-us/library/s1sb61xd%28VS.80%29.aspx :
"When modifying a variable or function at file scope, the static keyword specifies that the variable or function has internal linkage (its name is not visible from outside the file in which it is declared)."
请参阅此处,了解有关将其包含在头文件中的含义的更多信息:
关于c++ - [C++]带有 static 关键字的函数模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4348642/