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

  • SEARCH
  • Home
  • 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 4044304
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T13:14:14+00:00 2026-05-20T13:14:14+00:00

Thanks in advance for reading. This is a picky design question in that I

  • 0

Thanks in advance for reading. This is a picky design question in that I have an adequate solution, but question if there isn’t a better way via templates, which I’m not very experienced with.

I have a series of types of data tables (derived from abstract class DataTable) which I need to store. I wrote a “DataTableIndex” abstract class which stores a vector DataTable*’s and which handles the general work common to all DataTableIndexes — doing lookups, implementing a Proxy pattern so tables are only loaded if needed, error checking, etc.

I then subclass it for each table type, the only reason I need to do so is because each table type has a specific function to be called to load it.

I’d like to avoid this subclassing of DataTableIndex somehow via templates if possible, because there are lots of subclasses of DataTable.

class DataTableIndex
{  
  // various functions to implement lookup, Proxy and error checking
  //   functionality common to all DataTableIndexes.
  // This code needs access to _lookupTable and _theTables

  DataTable* getTable( int tableNum );

private:
  // These functions call the appropriate user interface function for loading
  //   a table of the subclass' type.
  //   They are called by more general non-virtual public functions
  virtual bool loadIndex( vector<LookupEntry*>& returnLookupTable ) = 0;
  virtual DataTable* loadTable( int tableNum ) = 0;

  vector<LookupEntry*> _lookupTable;
  vector<DataTable*>   _theTables;

  UserInterface* UI;
};  

This class has then very simple subclasses, which basically point to functions in the User Interface class that actually open and parse the file of the data table.

class TableTypeA_Index : public DataTableIndex
{
  virtual bool loadIndex( vector<LookupEntry*>& returnLookupTable )
  {
     return UI->loadTableAIndex( _lookupTable );
  }

  virtual DataTable* loadTable( int tableNum )  
  { 
     return UI->loadTableTypeA( _lookupTable[ tableNum ] );
  }

};

This works adequately. But I feel like I should be able to pass “loadTableTypeA” for example to DataTableIndex via a template parameter, so I don’t have to subclass it.

Another reason why I would like to use templates is that I don’t want to have to cast DataTable* all of the time to the actual table type. Even though I know at compile time what type of table it should be, I feel like I should be using dynamic_cast<> to error check, but I don’t want the caller of getTable() to have to do this every time (it is called often).

My ideal solution would:
1) generalize the DataTableIndex class to a template, with template parameters replacing LookupEntry* and DataTable* in _lookupTable and _theTables. This would eliminate casting.

2) map the appropriate UI functions to load the right table type, without subclassing.

So basically I’d like to the use of this class to look like this (somehow)

DataTableIndex< LookupEntryTypeAMatlab, 
                TableTypeA, 
                loadTableAIndex(),
                loadTableTypeA() > theTypeAIndex;

I gave some thought to policy classes, but my impression of that approach was that, in this case, I’d just be moving the subclassing to something else.

  • 1 1 Answer
  • 0 Views
  • 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-20T13:14:14+00:00Added an answer on May 20, 2026 at 1:14 pm

    In general, this could be done by using the strategy pattern. This can be implemented using simple composition, so there are no templates needed. However, you will still have to define a special strategy for each of the different table types. This will require subclassing:

    class LoadTableStrategy {
    public:
        virtual bool loadIndex( vector<LookupEntry*>& returnLookupTable ) = 0;
        virtual DataTable* loadTable( int tableNum ) = 0;
    };
    

    This class will have to be subclasses for each table-type. DataTableIndex would then receive an instance of LoadTableStrategy in the constructor, and use that to load the data, instead of the private virtual functions.

    You can of course pass the strategy-type as template parameter, but be aware, that this as drawbacks. Two DataTableIndex instantiated with different template parameters will be different types for the compiler. You will not be able to define a function that can handle both, unless you create specific overloads, or make the function a function template itself.

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

Sidebar

Related Questions

Thanks in Advance for reading and answer this question. I got button in asp
Hello everyone and thanks in advance for reading this.. I have the following html
I have read question : this and this about reading shared preferences. But they
And thanks in advance fro reading this. I am creating a page where the
Hello folks and thanks in advance for reading this. I currently use a slider
and thanks in advance for offering your time for reading this. I am newbie
thanks in advance for this excellent source of help. I've been reading a lot
After reading this discussion and this article , I still have this question. Let's
Thanks in advance for your help. I have a need within an application to
thanks in advance for looking at this. Essentially, I'm calling didInsertElement() and binding an

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.