From: ManicQin on
I'll try to explain my thoughts.
I had a newbie's mistake, in one "world" of my system I have an a
array from 0-89 that represents devices.
In the second world I had 90 devices indexed as 1-90.
all went good until I tried to access the index 90 in the array and
you can pretty much understand the rest. (CRUSH) Like I said a
newbie's mistake.

I thought about an automatic solution for this kind of problems and I
would really love your opinion
Offcourse I could just decrement every index by 1 but, hey you know
it's not fun ;)

I know it's slower than just increment decrement but I think it has
his pro
you can use it for more complicated transformations i.e. hashings and
working on classes
it decouples the trasformation from one index to another (i.e. one
system's range is from 0-9 and another is from 1-10)
with a few small and fast changes you can support cross type
transformations (hasing of a class into a pod).
waiting for your thoughts people.

//snip
#include <iostream>
using namespace std;

#define MAX_ITEMS 10
int Bam[MAX_ITEMS] = {0,1,2,3,4,5,6,7,8,9};

template<class _T>
class cTransFunc
{
public:
virtual _T operator ()(_T _V)
{
return _V - 1;
}
};

template<typename _T,class _F>
class cTransformatorTemp
{
public:
cTransformatorTemp(_T defVal)
{
m_iData = defVal;
}
operator const _T()
{
_F apply;
return apply(m_iData);
}
private:
_T m_iData;
};

typedef cTransformatorTemp<int,cTransFunc<int> > intConv;

int retPos(intConv inVal)
{
return Bam[inVal];
}

int main()
{ cout << retPos(5);
return 0;
}
\\snip

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]