Pac*_*ane 3 c++ syntax templates typename
我最近一直在做一个小项目,我无法弄清楚什么......
我已经使用typename模板给了一个包含类的.h文件.在那堂课里面有一个私人课程.
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
};
};
Run Code Online (Sandbox Code Playgroud)
当我想定义类"Something"的函数时出现问题
这是我的做法(在.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
}
Run Code Online (Sandbox Code Playgroud)
所以窃听部分在定义部分......我是否必须告诉编译器返回类型(在本例中为Node*)使用typename模板(如下所示:) typename Node*?那参数怎么样?typename const Node&?
所以基本上,我何时必须指定函数/参数使用模板?
谢谢你的时间.
因为Function1,你需要告诉编译器Node是什么 - 在这种情况下,它是一个嵌套类型Something<T>.因为它依赖于T(它是一个依赖名称),所以你需要告诉编译器它是一个类型,所以你必须把它写成typename Something<T>::Node.问题是,可能会有一些T对于这Something<T>::Node实际上不是一个类型(即如果部分专业Something<T>).
因为Index,你有什么是好的 - const T&只是对a的引用const T,编译器知道是什么T.
| 归档时间: |
|
| 查看次数: |
727 次 |
| 最近记录: |