I have a cache object that caches several different types of objects, as shown below:
class Cache
{
public:
ObjectTable<ObjTypeA> m_objACache;
ObjectTable<ObjTypeB> m_objBCache;
ObjectTable<ObjTypeC> m_objCCache;
};
The (terrible) way that I am currently using the cache currently directly accesses the properties of the cache class "m_objACache" and "m_objBCache" as follows:
Cache c;
c.m_objACache.getObjectWithid(objectBuffer, 1);
c.m_objACache.getObjectWithid(objectBuffer, 2);
c.m_objBCache.getObjectWithid(objectBuffer, 3);
etc..
What I would like 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 this does not work, because when I have a " ObjectTable<T>", I need a variable name, but I can’t change the template class variables - can I do this? Or this will be the case if you declare all the variables and access it as follows:
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.
, ObjectTable, , - , downcasting, , .
!