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
  • 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-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

I've searched for this a little but I have not gotten a particularly straight
This is a little out there but I have a customer object coming back
This might sound like a little bit of a crazy question, but how can
I have this little function function makewindows(){ child1 = window.open (about:blank); child1.document.write(<?php echo htmlspecialchars(json_encode($row2['ARTICLE_DESC']),
I have this little script to toggle a contact form when a button is
Sorry if this is a little off-topic for regular stackoverflow questions, but we're tearing
I'm trying to be responsible with my DOM references in this little Flash 8/AS2
ffdshow has this awesome little API for controlling playback of video files. It allows
This is a little confusing to explain, so bear with me here... I want
Sorry, I'm new to SVN and I looked around a little for this. How

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.