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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T01:28:13+00:00 2026-06-08T01:28:13+00:00

Setup class Base { public: Base(); virtual ~Base(); int getType(); protected: int type; };

  • 0

Setup

class Base
{
public:
    Base();
    virtual ~Base();
    int getType();
protected:
    int type;
};

class DerivedA : public Base
{
public:
    DerivedA() { this->type = 1; };
    ~DerivedA();

    int getA() { return 1;};
};

class DerivedB : public Base
{
public:
    DerivedB() { this->type = 2; };
    ~DerivedB();

    int getB() { return 2;};

};

Target

Having a vector containing objects of both derived classes and then be able to access child-specific methods.

Current “solution”

int main()
{
    typedef boost::ptr_vector<Base> BasePtr;
    BasePtr vec;

    // Fill vec with some stuff 
    vec.push_back(new DerivedA());
    vec.push_back(new DerivedB());
    vec.push_back(new DerivedA());
    vec.push_back(new DerivedB());

    typedef BasePtr::iterator BaseIter;

    for ( BaseIter it = vec.begin(); it != vec.end(); it++ ) {

       if (it->getType() == 1) {
          std::cout << it->getA() << '\n';
       } else {
          std::cout << it->getB() << '\n';
       }    

    }

    return 0;
 }

Problem

Obviously “it” is not recognised as either DerivedA or DerivedB, so the child-specific method cant be accessed.
Some form of cast is required, so i guess the question is:

How do I properly cast the iterator to the correct derieved class?

Maybe there is a better way to structure this whole scenario?

Edit:
Seems i was a bit unclear. The purpose of the methods in the derived classes is fundamentally different.
Consider the base class Item that have the derived classes Armor and Weapon.

In this example you can see why, for instance, Weapon have a function getDamage() that maybe returns a float.

This function is not needed for Armor and dosn’t even have anything similar.

In this example you can see the vector as an Inventory that can contain any number, and types, of items. Maybe even items that have a stack and some use (Potions maybe)

  • 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-08T01:28:14+00:00Added an answer on June 8, 2026 at 1:28 am

    If you have to cast to derived, then it means you have a broken design.

    But if you really have to then this would do (put it in the for loop) :

    DerivedB * typeB = dynamic_cast< DerivedB * >( &*it );
    if ( typeB != nullptr )
    {
      std::cout << typeB->getB() << '\n';
    } 
    

    A better approach would be to add getB() to the interface, and implement it in DerivedA (it can return some dummy value, or throw if really needed).

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

Sidebar

Related Questions

So I have this kind of setup class Base { public: Base(); virtual void
I have this basic setup: enum{ BASE, PRIMITIVE, ... }; class IUnknown{ public: bool
I Have something similar to this setup: public class Base { public String getApple()
I have this setup: class A { public: virtual void Function(); } class B
I have a repository pattern setup using NHibernate. The base class looks like this:
Here is my code: class MyTestCase(Base): def setUp(self): #some code here def test_B(self): #some
For my tests I'm using a base class MyTestBase defining a method setup() that
I have a two models set up like this: class User < ActiveRecord::Base #
If I have a simple class setup like this: class MyClass { private string
so with this class i have public class Options { public bool show {

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.