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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T19:31:54+00:00 2026-06-01T19:31:54+00:00

Sorry for confusing the topic, this was the original question: How to reallocate an

  • 0

Sorry for confusing the topic, this was the original question:

How to reallocate an array of pointers to base class, that actually
point to different derived classes?

I shouldnt use dynamic cast, typeid or RTTI here if possible..

EDIT I just realized i could try saving the array elements and just setting the pointers in new array to old elements.. But then how to do operator= or Cctor?

Or:

How to reallocate this array ‘the bad way’, by actually copying the elements?

An example:

class Base {...}
class Derived1 : public Base {...}
class Derived2 : public Base {...}

int main()
{
  int arrayLength=0, arrayMaxLength=3;

  Base **array=new Base*[arrayMaxlength];
  array[0]=new Derived1();
  array[1]=new Derived2();

  //Reallocation starts...
  Base **tmp=new Base*[arrayMaxLength*=2];

  for(int i=0;i<arrayLength;i++)
    tmp[i]=new Base(*array[i]);      //<------ What to put here instead of Base?


  //The unimportant rest of Reallocation..
  for(int i=0;i<arrayLength;i++)
    delete array[i];
  delete [] array;
  array=tmp;
}
  • 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-01T19:31:56+00:00Added an answer on June 1, 2026 at 7:31 pm

    You should not need to do any casting (dynamic or otherwise): just reuse the pointers

    Base **array=new Base*[arrayMaxlength]; 
    array[0]=new Derived1(); 
    array[1]=new Derived2(); 
    
    //Reallocation starts... 
    Base **tmp=new Base*[arrayMaxLength*=2]; 
    
    for(int i=0;i<arrayLength;i++) 
      tmp[i]=array[i]; 
    
    // ...
    

    The objects are already allocated and have the proper type. You can just copy the pointers themselves to the new array.

    Note that you will have to be careful not to delete the (objects pointed to by the) pointers in the old array, as that would invalidate the ones in the new (the object pointed to by the stored pointers does not exist any more) — just delete the original array iself.

    If the management of pointers becomes burdensome, you can use some sort of shared pointer (the BOOST library offers a variety, for example)

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

Sidebar

Related Questions

Sorry if this question is confusing. I have inherited a site that is already
Sorry for the confusing title. Basically I made a BinaryReader class that reads in
Sorry if that question is confusing, I'm self-learning javascript. I'm dynamically generating image thumbnails
Sorry for the confusing question, but what I'm trying to do is store an
Sorry if the title sounds confusing - but this is what I am trying
Sorry if the question title is confusing. Let me explain further. I am building
Sorry for such a silly question but Python docs are confusing... Link 1: Queue
Sorry if this is a dup; I haven't found any questions that pose quite
Sorry for the confusing title :P I'm sure I should know this but I
Hello and sorry for the confusing title of my question. My app assigns a

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.