template <typename> class Vector;
template <typename T>
bool operator==(const typename Vector<T>::const_iterator&, const typename Vector<T>::const_iterator&); // 友元声明,签名是这么写吗?
template <typename T>
class Vector {
public:
class const_iterator: {
friend bool operator==(const const_iterator& lsh, const const_iteraotr& rhs);
public:
};
};
template <typename T>
bool operator==(const typename Vector<T>::const_iterator& lhs, const typename Vector<T>::const_iterator& rhs) { // 友元定义
}
如果直接将==友元在const_iterator
里面定义,比较简单,直接写就行。但是我想在类外定义时,就不知道怎么写它的函数签名了。
高洛峰2017-04-17 14:49:42
It is best to write it like this, what you wrote above seems difficult to achieve
Reference
template <typename> struct Vector;
template <typename T>
struct Vector_const_iterator;
template <typename T>
bool operator==(const Vector_const_iterator<T>& lsh, const Vector_const_iterator<T>& rhs);
template <typename T>
struct Vector {
typedef Vector_const_iterator<T> const_iterator;
};
template <typename T>
struct Vector_const_iterator {
friend bool operator == <> (const Vector_const_iterator<T>& lsh, const Vector_const_iterator<T>& rhs);
};
template <typename T>
bool operator==(const Vector_const_iterator<T>& lsh, const Vector_const_iterator<T>& rhs) {
return true;
}
PHP中文网2017-04-17 14:49:42
There is something wrong with your function declaration. Friends only have access rights, not members of the class.
template <typename T>
class Vector {
public:
class const_iterator{
friend bool operator==(const typename Vector<T>::const_iterator& lsh,
const typename Vector<T>::const_iterator& rhs);
public:
};
};
template <typename T>
bool operator==(const typename Vector<T>::const_iterator& lhs, const typename Vector<T>::const_iterator& rhs) { // 友元定义
return false;
}