我一直在一个小项目,最近的工作时,我无法弄清楚的东西我使用关键字“类型名称” ..什么时候应该使用模板
我已经拥有一个.H文件包含一个类,使用一个typename模板。在那个班里面有一个私人班。
template <typename T>
class Something
{
public:
Something();
~Something();
Node* Function1(int index);
int Index(const T& id);
private:
class Node()
{
public:
T id;
//Imagine the rest for the Node
};
};
问题发生时,我想定义(在.INL文件)
template<typename T>
Node* Something::Function1(int index) //Is the return type well written?
{
// returns the node at the specified index
}
template<typename T>
int Something::Index(const T& id) //Is the parameter type well specified?
{
// returns the index of the node with the specified id
}
所以窃听类的“东西”
以下是我在做它的功能部分在定义部分...我是否必须告诉编译器返回类型(在这种情况下是Node *)使用typename模板(如下所示:typename Node*
)?那么参数呢? typename const Node&
?
所以基本上,我什么时候必须指定更多的功能/参数使用模板?
谢谢你的时间。
请将sinppets发布到您的问题中! – mmmmmmmm 2010-11-19 17:45:09
我已经发布了片段的链接。我无法弄清楚如何正确缩进我的片段。 – Pacane 2010-11-19 17:45:47