> The definition of the operator function cannot be inside of the class because
> of it's second parameter
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
template<typename T, typename A>
class Pair {
public:
Pair(T p1, A p2) :pair1(p1), pair2(p2) { std::cout << "Constructing pair...\n"; }
~Pair() { std::cout << "Destroying pair...\n"; }
friend std::ostream& operator<<(std::ostream& out, const Pair<T,A>& obj)
{
return out << obj.pair1 //c++, where a friend has access to your private members.
<< " " << obj.pair2 << std::endl;
}
T get_pair1() const { return pair1; }
A get_pair2() const { return pair2; }
private:
T pair1;
A pair2;
};
|
however, there are more interesting things to do that only print state.
> looking at the vector example he provides, we're still breaking it in in
> effectively the same manner, just without an ugly get() or set() function,
> and instead replacing those with operator[], so what's the difference, you know?
A container simply contains objects, and so, it ought to provide some form of access to said objects.
You should note that it does not expose its implementation details.
Perhaps a vector is too simply, take a list instead. You may access the objects, but no the nodes/cells. You may not modify the `next' or `previous' pointers, but you may split and merge.