Hi, I have a cache object that caches a number of different types of objects, as illustrated below:
class Cache
{
public:
ObjectTable<ObjTypeA> m_objACache;
ObjectTable<ObjTypeB> m_objBCache;
ObjectTable<ObjTypeC> m_objCCache;
};
The (horrible) way I'm currently using the cache at the moment is directly accessing the cache class properties "m_objACache" and "m_objBCache" like this:
Cache c;
c.m_objACache.getObjectWithid(objectBuffer, 1);
c.m_objACache.getObjectWithid(objectBuffer, 2);
c.m_objBCache.getObjectWithid(objectBuffer, 3);
etc..
What I'd like to be able to do is something like this : -
class Cache
{
public:
template <typename T>
void getObjectWithId(T &objectBuffer, int id)
{
ObjectTable<T>.getObjectWithId(objectBuffer, id);
}
};
But obviously that does not work because where I have "ObjectTable<T>
" I need a variable name, but I cannot template class variables - so is there a way I can do this? Or is it going to be a case if declaring all of the variables and accessing it like this:
class Cache
{
public:
void getObjectWithId(ObjTypeA &objectBuffer, int id)
{
m_objACache.getObjectWithId(objectBuffer, id);
}
void getObjectWithId(ObjTypeB &objectBuffer, int id)
{
m_objBCache.getObjectWithId(objectBuffer, id);
}
void getObjectWithId(ObjTypeC &objectBuffer, int id)
{
m_objCCache.getObjectWithId(objectBuffer, id);
}
protected:
ObjectTable<ObjTypeA> m_objACache;
ObjectTable<ObjTypeB> m_objBCache;
ObjectTable<ObjTypeC> m_objCCache;
};
Which seems very verbose..
Each object type that an ObjectTable can be used for has a common base class, so there could be some other way of doing this that may inevitably involve downcasting, but I'm hoping I can find a better way.
Thanks!