첫 번째 경우에는 거꾸로했습니다. 당신이 반환 유형을 지정하는 경우, 템플릿 식별자 (Container<T>
)로 템플릿 매개 변수의 목록을 포함해야하지만, 당신은 매개 변수 유형을 지정하는 경우, 당신이 그것을 할 필요가 없습니다
template <typename T>
const Container<T> & Container<T>::operator=(const Container & rightCont){
...
(단지
Container
충분하다)
다른 이유가 있습니다.
두 번째 경우에 operator ==
을 친구로 선언하면이 경우 operator ==
은 일반적인 기능이라는 것을 경고합니다. 템플리트의 특수화가 될 수 없습니다. 나는. 클래스 Container<int>
의 경우
bool operator==(const Container<int> &rhs, const Container<int> &lhs) {
// ...
}
은 (는) 친구가됩니다. 그러나 U == int
에 대한 기능 템플릿
template <class U>
bool operator==(const Container<U> &rhs, const Container<U> &lhs) {
// ...
}
의 전문화 하지는 Container<int>
의 친구가 될 것입니다. 그것이 당신의 의도라면, 당신은 괜찮습니다.위의 템플릿의 특정 전문화 친구가하고 싶었 경우 에게 위의 템플릿의 모든 전문화 친구가하고 싶었다면
, 당신은
template <typename T>
class Container {
friend bool operator==<T>(const Container<T> &rhs, const Container<T> &lhs);
...
말해야 할 것입니다, 당신은 할 것 예 :
template <typename T>
class Container {
template <class U>
friend bool operator==(const Container<U> &rhs, const Container<U> &lhs);
...
출처
2010-05-15 06:12:20
AnT