Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 973201
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T03:14:38+00:00 2026-05-16T03:14:38+00:00

I have a cache object that caches a number of different types of objects,

  • 0

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!

  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T03:14:39+00:00Added an answer on May 16, 2026 at 3:14 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a object cache class like this: #include boost/thread/mutex.hpp #include boost/unordered_map.hpp template <typename
I have a controller object implemented as a singleton, which has a cache that
I have a cache which has soft references to the cached objects. I am
I just started using SVN, and I have a cache directory that I don't
I have been looking into different systems for creating a fast cache in a
I have code in global.asax that sets some things in cache with CacheItemRemovedCallback. When
I have an application that can be used without authentication on computers in public
I have a model object which did not have a counter cache on it
I have an abstract class called DatabaseRow that, after being derived and constructed, is
I'm working on an iPhone app that gets a number of objects from a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.