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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T05:46:18+00:00 2026-06-15T05:46:18+00:00

I am working on a C++ project, specifically implementing a shunting yard algorithm. I

  • 0

I am working on a C++ project, specifically implementing a shunting yard algorithm.

I have a function that creates a vector of shared_ptr‘s of type super class, but the classes that are being pushed into this vector are all base class shared_ptrs.

I then need to take this vector and pass it into another function and carry out different logic for each element of the vector in a for loop. The logic that I carry out, however, depends on which base class is present in each element of the vector.

So basically what I don’t know is how to identify which type of base class is in each element of the vector. When I debug they are all coming out as type super-class.

So generally I’d like to do something like this:

if(vectorElement == baseClass)
{
  //do some logic
}

Or if there is some different method of carrying this out which I’m missing I’d be interested in carrying that out.

  • 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-06-15T05:46:19+00:00Added an answer on June 15, 2026 at 5:46 am

    There are many solutions to your problem, frankly is the almost most common problem in OOP.

    The most obvious is the virtual function doing different things in different classes:

    class SuperClass {
    public:
       virtual void doSomething(/*someArgsIfNeeded*/) {
          // some base implementation, possible empty
          // or just use pure virtual function here
       }
    };
    
    class SubClass : public SuperClass {
    public:
       virtual void doSomething(/*someArgsIfNeeded*/) {
          // some base implementation
       }
    };
    

    Then use it as follows:

    int SomeArgs;
    std::vector<std::shared_ptr<SuperClass>> objects;
    for (auto it = objects.begin(); it != objects.end(); ++i)
      it->doSomething(/*someArgsIfNeeded*/);
    

    Other more sophisticated solution is to use visitor pattern.


    It is considered a bad practice to use casting (dynamic_cast), so always search for more OO solutions than casting, like these two I presented above.

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

Sidebar

Related Questions

I have been working on a project that has common bits of functionality, specifically
I have a working project that Im amending, it crashes after trying to use
I am working on a project that requires the manipulation of enormous matrices, specifically
I am working on a geolocation project that is browser-based and geared specifically towards
When working in a VB.NET project in Visual Studio (2008 specifically, but hopefully the
I working on project and have problem with threading and update of UI. I
I have a working C2DM project, atm. I have me and my colleagues phones
I have a project working fine under MSVS 2010 SP1. I'm trying to convert
I am working on project where I have the checkInternet method in one of
For a personal project I have been implementing my own libstdc++. Bit by bit,

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.