- May 21, 2013
- 3,656
- 60
- 91
Maybe my approach is completely wrong, maybe I'm just missing something. But I cannot figure out for the life of me what I'm doing wrong or not doing.
I have a template class, Pair. It holds two variables of the same type T. I have overloaded the insertion operator as a friend function.
I have a partial specialization of Pair, namely Pair<T*>. I still want to be able to use the << operator on objects of this type.
I know I can't partially specialize the friend function. That would have been too easy, anyway, I guess. I tried adding an extra layer of abstraction wherein the friend function simply calls a private print() function. The problem is that this function is private. If I get rid of the friend function in the partially specialized class, I get an access error. I would prefer it not be public, because I want people to use the operator.
I have not tried going full non-member, non-friend yet with the operator overload because I would also rather not have simple getters in my class.
I would also prefer not to make the all possible instances friends, as it provides too much access.
It seems like I would have to go even more abstract: define an interface class, derive both types from the interface (T and T*). But then I'm stuck dealing in pointers to the interface.
I'm not sure what I'm missing, if anything. And if I have to choose one of the less desirable routes, which one is "best"?
Here's the code:
I have a template class, Pair. It holds two variables of the same type T. I have overloaded the insertion operator as a friend function.
I have a partial specialization of Pair, namely Pair<T*>. I still want to be able to use the << operator on objects of this type.
I know I can't partially specialize the friend function. That would have been too easy, anyway, I guess. I tried adding an extra layer of abstraction wherein the friend function simply calls a private print() function. The problem is that this function is private. If I get rid of the friend function in the partially specialized class, I get an access error. I would prefer it not be public, because I want people to use the operator.
I have not tried going full non-member, non-friend yet with the operator overload because I would also rather not have simple getters in my class.
I would also prefer not to make the all possible instances friends, as it provides too much access.
It seems like I would have to go even more abstract: define an interface class, derive both types from the interface (T and T*). But then I'm stuck dealing in pointers to the interface.
I'm not sure what I'm missing, if anything. And if I have to choose one of the less desirable routes, which one is "best"?
Here's the code:
Code:
#ifndef PAIR_HPP
#define PAIR_HPP
#include <iostream>
// Prep friend function template
template <typename T> class Pair;
template <typename T>
std::ostream& operator <<(std::ostream& sout, const Pair<T> &rhs);
template <typename T>
class Pair {
public:
Pair();
Pair(T one, T two);
friend std::ostream& operator << <T>(std::ostream& sout, const Pair<T> &rhs);
private:
T first;
T second;
std::ostream& print(std::ostream& sout) const;
};
// Class implementation
template <typename T>
Pair<T>::Pair() : first(T()), second(T()) { }
template <typename T>
Pair<T>::Pair(T one, T two) : first(one), second(two) { }
template <typename T>
std::ostream& operator <<(std::ostream& sout, const Pair<T> &rhs)
{
return rhs.print(sout);
}
template <typename T>
std::ostream& Pair<T>::print(std::ostream& sout) const
{
return sout << "(" << first << ", " << second << ")";
}
#include "ptr_Pair.inl"
#endif