Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
409 views
in Technique[技术] by (71.8m points)

c++ - How do I select a member variable with a type parameter?

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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Like this perhaps?

class Cache
{
 // An "envelope" type which up-casts to the right ObjectTable<T> 
 // if we have a type parameter T. 
 struct ObjectTables : ObjectTable<ObjTypeA>,  
                       ObjectTable<ObjTypeB>, 
                       ObjectTable<ObjTypeC> {};

 ObjectTables tables; 
public:

    template <typename T>
    void getObjectWithId(T &objectBuffer, int id)
    { 
        // C++ does the work here
        ObjectTable<T> &o=tables;
        t.getObjectWithId(objectBuffer, id);
    }
};

Also, it's easy to extend. Just throw in more ObjectTables<> if you need to support more types.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...