我有我已经写其[]操作的一类,我想,有时操作者会返回一个INT有时一结构。重载类的操作符[]在C++
但编译器不会让我重载操作符,为什么?
它说:“......不能超载”
代码:
template <class T> struct part
{ };
template <class T> class LinkedList
{
public:
LinkedList() : size(0), head(0) {}
T& operator[](const int &loc);
part<T>& operator[](const int &loc);
};
template <class T> T& LinkedList<T>::operator[](const int &loc)
{
..a lot of thing which compiles perfectly
}
template <class T> part<T>& LinkedList<T>::operator[](const int &loc)
{
...the same thing but returns struct&.
}
而你的代码看起来像......? (函数怎样才能有两种不同的返回类型?) –
你能提供一些代码给我们看看你试过了什么吗? – DeCaf
函数可以具有不同的返回类型,只要它的输入类型(或某个模板参数)可以消除歧义。 –