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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T12:56:19+00:00 2026-06-14T12:56:19+00:00

How can I create a dynamic array of objects polymorphically, if I have an

  • 0

How can I create a dynamic array of objects polymorphically, if I have an abstract class with derived classes, and without using STL data structure classes? (vectors, list, etc)

A static array of objects

TwoDimensionShape *shapes[2];       
shapes[0] = &Triangle("right", 8.0, 12.0);  
shapes[1] = &Rectangle(10);  

I know I can’t do this because you cannot create objects of an abstract class:

cin >> x;
TwoDimensionShape *s = new TwoDimensionShape [x];

EDIT:

Thanks to Nick, this works:

  int x = 5;
  TwoDimensionShape **shapes = new (TwoDimensionShape*[x]);
  • 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-14T12:56:20+00:00Added an answer on June 14, 2026 at 12:56 pm

    You can create an array of pointers to that class:

    TwoDimensionShape **s = new TwoDimensionShape*[x];
    

    And then construct each object with it’s specific type:

    s[0] = new Triangle("right", 8.0, 12.0);  
    s[1] = new Rectangle(10);
    

    Similar to what you had.
    Remember to delete when you don’t need anymore.

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

Sidebar

Related Questions

I can create and use dynamic two dimensional array in Fortran (in 77 standard).
I'm trying to create a dynamic array of objects, similar to ArrayLists in Java.
I have a container class which receives a pointer to a dynamic array from
how can I create a dynamic lambda expression to pass to use in my
Most things are dynamic in SSRS i.e you can create a custom expression for
I want to create a component which can be used like: <mc:chart data=#{bean.data} width=200
I'm building an application which will have dynamic allocated objects of type A each
Well... I am trying to create an application using the TTS Engine. I can
I want to have a class with dynamic methods. From these arrays: prefix =
What would be the best way to create a dynamic array for a user

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.