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 364557
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T13:27:00+00:00 2026-05-12T13:27:00+00:00

I have a class, say, CDownloader, that reads some XML data and provides access

  • 0

I have a class, say, “CDownloader”, that reads some XML data and provides access by node names. It features some getter functions, something like this:

BOOL CDownloader::getInteger ( const CString &name, int *Value );
BOOL CDownloader::getImage   ( const CString &name, BOOL NeedCache, CImage *Image );
BOOL CDownloader::getFont    ( const CString &name, CFont *Font );

I cannot change CDownloader class. Instead I would like to write some functions, that downloads items by using a bool flag, not an actual name. Something like this:

BOOL DownloadFont( const CDownloader &Loader, bool Flag, CFont *Font )
{
   if (Flag) {
      // first try the "name_1"
      if ( Loader.getFont("name_1", Font) ) return TRUE;
   }
   // if "name_1" fails or disabled by flag, try "name_2"
   return Loader.getFont("name_2", Font);
}

I can write Download(Font|Integer|Image) functions separatly, but this will result in code duplication. My idea is to write a template, but I am still at a loss: how can I determine what method should I call from CDownloader class? To specialize template for each data type means to stuck into code duplication again. To pass getter funciton as a “pointer-to-function” parameter? But the getter signatures differ in CDownloader…

Summing it up, the question is: is it possible to write a generic wrapper around CDownloader or do I have to duplicate code for each “get***” function? Thanks in advance!

  • 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-12T13:27:00+00:00Added an answer on May 12, 2026 at 1:27 pm

    As long as you have three differently named functions and need to pick one depending on the type, at some point you have to have either an overload or some traits class to picks the right one. I don’t think there’s a way around that. However, since the call to one of these function is the only thing that needs this, if there is more code to these DownloadXXX() functions than you showed us, then it might still make sense.

    Here’s a sketch of what you might do using the overload alternative. First you need three overloads of the same function each calling one of the three different functions. The additional BOOL parameter for one of the functions somewhat wreaks havoc with the genericity, but I got around that by having all functions accept that BOOL, but two of them ignoring it:

    inline BOOL Load(CDownloader& Loader, const CString &name, int &Value, BOOL)
    {return Loader.getInteger(name, &Value);
    
    inline BOOL Load(CDownloader& Loader, const CString &name, CImage &Value, BOOL NeedCache)
    {return Loader.getImage(name, NeedCache, &value);
    
    inline BOOL Load(CDownloader& Loader, const CString &name, CFont &Value, BOOL)
    {return Loader.getFont(name, &Font);
    

    Now you can go and write that generic function. You need to decide what to do about that BOOL, though:

    template< typename T >
    BOOL Download(const CDownloader &Loader, bool Flag, T &Obj, BOOL NeedCache /*= true*/)
    {
       if (Flag) {
          if ( Load(Loader, "name_1", Obj, NeedCache) ) return TRUE;
       }
       return Load(Loader, "name_1", Obj, NeedCache);
    }
    

    However, as you can see, this is really only worth the hassle if that Download function is a lot more complicated than in your sample code. Otherwise the added complexity easily outweighs the gains that the increased genericity brings.

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

Sidebar

Related Questions

No related questions found

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.