我是新来的全职C++编程,所以我试图更好地理解各种细节中涉及的细微差别。使用模板的C++方法定义
我在一个小型项目中使用模板,主要是在学习时编写代码,而且我遇到了一些我不确定的事情。 Visual Studio的帮助我产生(在我的.cpp文件从我的.h文件)的代码等价于:
template<class T>
PriorityQueue<T>::ClimbDownHeap(const int currentNodeIndex)
{
}
template<class T>
PriorityQueue<T>::GetRightNodeIndex(const int currentNodeIndex)
{
}
我的印象,虽然这将是同样有效:
template <class T>
class PriorityQueue
{
public:
ClimbDownHeap(const int currentNodeIndex)
{
}
private:
GetRightNodeIndex(const int currentNodeIndex)
{
}
};
我的理解可能是错误的,但至少目前为止,似乎两者都可以编译。这两种风格之间是否有重大差异?我更喜欢第二个,因为它对我来说更加干净明了。这些之间的细微差别是什么?
注意:在颠簸的火车上打字时,如果格式问题或代码不清楚(我从我尝试过的内存中输入它,它不是确切的),我很抱歉。
将模板定义放入源文件可能会有问题。您可以阅读[this](http://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file)问题以了解更多信息。 –
请参阅[为什么模板只能在头文件中实现?](http://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file) –
I可以推荐** CppCon 2016的演讲:Arthur O'Dwyer“Template Normal Programming”** [Part1](https://www.youtube.com/watch?v=vwrXHznaYLA)和[Part2](https:// www.youtube.com/watch?v=VIz6xBvwYd8) –