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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T08:29:27+00:00 2026-05-16T08:29:27+00:00

This is a little contrived, but say I have a class interface like this:

  • 0

This is a little contrived, but say I have a class interface like this:

class IResource;
class IResourceContainer
{
 public:
    virtual ~IResourceContainer() {}
    virtual void AddResource(const std::string& rStrName, 
                             std::auto_ptr<IResource> apResource)=0;
    virtual IResource& GetResource(const std::string& rStrName)=0; 
};

and I have an implementation of this class which contains a map of strings to IResource types. If I were to add my own resource like this:

container.AddResource("foo", std:auto_ptr<IResource>( new CFooResource);

and then later retrieve the resource reference

CFooResource& fooResource = container.GetResource(); // error

This wouldn’t compile since I would need to downcast the IResource to a CFooResource. I thought about hiding this by making GetResource take a template parameter which downcasts the type internally, but obviously, templates and pure interfaces don’t jive. My current alternative is to hide the casting in a CastResource function which calls boost::polymorphic_downcast, but I’m still not thrilled with the idea that a client will need to cast the resource.

For example:

CFooResource& fooResource = CastResource<CFooResource&>(container.GetResource());

So I guess my question is: is there a better way of holding pointers to generic types that don’t require explicit downcasts from the user? I feel like there’s a templated way of doing this, but I’m not seeing it. Also, I made this interface so that clients could easily mock it out in their tests if need be.

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-16T08:29:28+00:00Added an answer on May 16, 2026 at 8:29 am

    is there a better way of holding pointers to generic types that don’t require explicit downcasts from the user?

    No.
    Either you’re using classic OO, a.k.a. run-time polymorphism. Then you are stuck with base class interfaces or you have to cheat and down-cast. Or you use templates, a.k.a. compile-time polymorphism. Then you are bound to a single resource type at compile-time.

    There are ways to blur the border between the two a little bit (boost::any, for example), but basically these are the two between you have to decide.

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

Sidebar

Related Questions

have written this little class, which generates a UUID every time an object of
found this little code snippet that seems to do what i want, but im
Given this little fiddle: http://jsfiddle.net/5Sw3h/8/ I have a navigation panel to the left, a
I have taken this little snippet straight out of some code I'm working on:
I have this little rake task: namespace :db do namespace :test do task :reset
I have this little problem, that I cannot figure out which arguments to pass
This question might be a little specific but the test program im writing uses
I need a little guidance here... I have 2 links: <div class=tarjBodyHolder> <div class=imageHolder>
I have a YAHOO Panel object defined like this: profilesDialog = new YAHOO.widget.Panel(profiles_dialog, {
I have a fun little problem where I'm presented with a logic clause like

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.