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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T13:53:31+00:00 2026-05-16T13:53:31+00:00

I’m trying to write a function for a database class that is basically just

  • 0

I’m trying to write a function for a database class that is basically just a wrapper around a hash_map of objects (say shapes) indexed by ID numbers that will look up an ID and cast it to the appropriate pointer type.

e.g. I’d like to be able to do something like this:

Circle* shapeToLookup = NULL;
int idNum = 12;
database.lookup(idNum, circleToLookup);
if(circleToLookup != NULL)
{
    // Do stuff with the circle.
}

and have the database know the type of its argument. Is there a way to do this without either overloading the function (lookup(int, Circle*), lookup(int, Rect*), ad nauseum)? Can you declare a function like lookup(int, Shape*) and have it know which type it’s given?

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-16T13:53:32+00:00Added an answer on May 16, 2026 at 1:53 pm

    You can do it with a template.

    Edit: new implementation based on the extra information. If mymap is a std::map<int, Shape*>:

    template <typename T>
    void lookup(int idNum, T* &ptr) {
        auto it = mymap.find(idNum);
        if (it == mymap.end()) {
            ptr = 0;
        } else {
            ptr = dynamic_cast<T*>(*it); // Shape must have a virtual member function
        }
    }
    

    Or you might prefer:

    template <typename T>
    T* lookup(int idNum) {
        auto it = mymap.find(idNum);
        if (it == mymap.end()) {
            return 0;
        }
        return dynamic_cast<T*>(*it);
    }
    

    Then call it like Circle *circle = database.lookup<Circle>(123);

    Obviously polymorphic containers are a whole heap of fun in themselves, but I’ll assume you have that sorted. There may well be a shared_ptr in there somewhere that I’ve left out.

    Old implementation when I thought the DB might store copies of POD:

    template <typename T>
    void lookup(int idNum, T* &ptr) {
        void *theresult = // something based on idNum
    
        // some check needed here that theresult really is the right type.
        // how you do this depends on the database, but suppose that
        // the database gives us some integer "type" which indicates the type
        if (type != type_constant<T>::value) {
            ptr = 0;
        } else {
            ptr = static_cast<T*>(theresult);
        }
    }
    

    type_constant is an example of “type traits”, you implement it like:

    template <typename T>
    struct type_constant {};
    
    template <>
    struct type_constant<Circle> {
        static const int value = 1;
    };
    
    template <>
    struct type_constant<Rectangle> {
        static const int value = 2;
    };
    
    // etc...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and

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.