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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T00:41:11+00:00 2026-05-11T00:41:11+00:00

I know downcasting like this won’t work. I need a method that WILL work.

  • 0

I know downcasting like this won’t work. I need a method that WILL work. Here’s my problem: I’ve got several different derived classes all from a base class. My first try was to make an array of base class. The program has to select (more or less at random) different derived classes. I had tried casting from a base class to the derived class, putting it in the array of the base, but obviously that didn’t work. I was sincerely hoping for another method than simply sticking arrays of all the derived classes, because there could be quite a few derived classes. Is there any better way to do this that I’m just brainfarting on?

If y’all need code examples or more information, just let me know. It all makes sense to me, but It’s late and it may not make sense to everybody else heh.

Any help is very much appreciated, guys.

  • 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. 2026-05-11T00:41:12+00:00Added an answer on May 11, 2026 at 12:41 am

    Not sure what you mean. Sounds like you store the objects by value, and you you have an array of Base. That won’t work, because as soon as you assign a Derived, that object will be converted to a Base, and the Derived part of the object is sliced away. But i think you want to have a array of pointers to base:

    Base * bases[NUM_ITEMS]; for(int i=0; i<NUM_ITEMS; i++) {     int r = get_random_integer();     if(r == 0)         bases[i] = new Derived1;     else if(r == 1)         bases[i] = new Derived2;     // ... } 

    If you ever haved worked with pointers, you will know it’s a pain in the ass to manage them, espacially pass around and not lose them, since you will need to call delete on them to free the memory and call the destructor of the objects. You can use shared_ptr, and it will manage that for you:

    shared_ptr<Base> bases[NUM_ITEMS]; for(int i=0; i<NUM_ITEMS; i++) {     int r = get_random_integer();     if(r == 0)         bases[i].reset(new Derived1);     else if(r == 1)         bases[i].reset(new Derived2);     // ... } 

    Now, you can pass bases[x] to another shared_ptr, and it will note you have got more than one reference – it will call automatically delete if the last reference to the objects go out of scope. Ideally, you would also replace the raw array by std::vector:

    std::vector< shared_ptr<Base> > bases; for(int i=0; i<NUM_ITEMS; i++) {     int r = get_random_integer();     if(r == 0)         bases.push_back(shared_ptr<Base>(new Derived1));     else if(r == 1)         bases.push_back(shared_ptr<Base>(new Derived2));     // ... } 

    Then you can pass the vector around, and don’t lose the size of it, and you can dynamically add items to it on demand. Get the size of the vector using bases.size(). Read about shared_ptr here.

    Conversion from a Base class to a Derived class should only be done when absolutely necessary. Normally, you want to use a technique called polymorphism, which means you call a function on the base pointer, but it will actually call a function defined in the derived class, having the same signature (name and parameters are the same type) and is said to override it. Read the article on wikipedia about it. If you really need to cast, you can do it like this for a raw pointer:

    Derived1 * d = &dynamic_cast<Derived1&>(*bases[x]); 

    Using dynamic_cast ensures, that when you cast to the wrong type (i.e the type you cast is not the type that was created and assigned to the base pointer), you get an exception thrown by the operator. For the shared_ptr case, there are ways too:

    shared_ptr<Derived1> d = dynamic_pointer_cast<Derived1>(bases[x]); if(d) {     // conversion successful, it pointed to a derived. d and bases[x] point still      // to the same object, thus share it.  } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 98k
  • Answers 98k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Well, unless you particularly want to disagree with Brian Goetz… May 11, 2026 at 7:32 pm
  • Editorial Team
    Editorial Team added an answer Your example code should work, but perhaps the problem is… May 11, 2026 at 7:32 pm
  • Editorial Team
    Editorial Team added an answer Check this (FxCop) and this (Reflector) out. In FxCop, you… May 11, 2026 at 7:32 pm

Related Questions

I know downcasting like this won't work. I need a method that WILL work.
For a system I need to convert a pointer to a long then the
I have this code to represent bank: class Bank { friend class InvestmentMethod; std::vector<BaseBankAccount*>
Let's say I have the following class structure: class Car; class FooCar : public
I know in certain circumstances, such as long running processes, it is important to

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.