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

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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:26:17+00:00 2026-05-27T20:26:17+00:00

Lets say I have an abstract base class and a derived class with some

  • 0

Lets say I have an abstract base class and a derived class with some unique functionality:

class Shape
{
    public:
    Shape();
};

class Circle : public Shape
{
    public:
    Circle(int radius) : radius(radius);
    int getRadius() { return this->radius; };
    private:
    int radius;
};

Now I can put Circle*:s in std::vector and check afterwards if vector has Shape*

std::vector< Circle* > v;

Circle* circle = new Circle(1);

v.push_back(circle);

// ...

Shape* someShape = ...; // I know it's a Circle but the pointer comes from class that only knows about Shapes

for(std::vector< Circle* >::iterator it = v.begin(); it != v.end(); ++it)
{
    Circle* c = *it;
    if((Shape)c == someShape)
    {
        // found
    }
}

// ...

delete c;

But I want to use std::unordered_set (applies also to std::unordered_map, std::set, std::map) because it has faster-than-linear find() and count()

std::unordered_set< Circle* > u;

Circle* circle = new Circle(1);

u.insert(circle);

// ...

Shape* someShape = ...; // I know it's a Circle but the pointer comes from class that only knows about Shapes

if(u.count(someShape) == 1)
{
    // found
}

Instead of the obivious I get “undefined function” as there only exists std::unordered_set< Key >::count(Key& k);

Is there any way I can find() or count() from std::unordered_set using base class?

  • 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-27T20:26:18+00:00Added an answer on May 27, 2026 at 8:26 pm

    If you know it’s a circle then you can simply cast someShape to Circle * and pass that to find().

    Shape* someShape = ...; // I know it's a Circle but the pointer comes from class that only knows about Shapes
    Circle* someCircle = dynamic_cast<Circle*>(someShape);
    
    if (someCircle != NULL && u.count(someCircle) == 1)
    {
        // found 
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For simplicity's sake, lets say I have the following Abstract Base Controller Class: public
Lets say have this immutable record type: public class Record { public Record(int x,
Let's say I have this base class: abstract public class Base { abstract public
Let's say in some abstract ViewModel base-class I have a plain-old property as follows:
Lets say I have an abstract base class with a pure virtual that returns
Lets say I have the following code: abstract class Animal case class Dog(name:String) extends
Lets say i have the following c# classes: abstract class a { protected abstract
Let's say I have some classes like this: abstract class View(val writer: XMLStreamWriter) {
Let's say I have defined the following class: public abstract class Event { public
Let's say I have a Java class abstract class Base { abstract void init();

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.