In my ongoing adventure with templates, I template my Container class not only on the ItemType that it has, but also on the Functor argument, which determines how it should arrange the elements. So far so good.
A small problem that I encountered occurs when I want to copy the contents of one container to another: if two containers have different Functor types, then they are technically unrelated classes. Thus, container A is not allowed access to the non-public contents of container B. Is there a good way to handle this problem, except to do everything I need to access the public? Some kind of "friend" ad template perhaps?
Sample code to demonstrate the problem:
#include <stdio.h>
class FunctorA {};
class FunctorB {};
template <class ItemType, class Functor> class MyContainer
{
public:
MyContainer() : _metaData(0) {}
template<class RHSFunctor> void CopyFrom(const MyContainer<ItemType, RHSFunctor> & copyFrom)
{
_metaData = copyFrom._metaData;
_item = copyFrom._item;
}
private:
int _metaData;
ItemType _item;
};
int main(int argc, char ** argv)
{
MyContainer<void *, FunctorA> containerA;
MyContainer<void *, FunctorB> containerB;
containerA.CopyFrom(containerB);
return 0;
}
source
share