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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T01:52:28+00:00 2026-06-19T01:52:28+00:00

So I have a dynamically allocated array of base class. I’ve stored a few

  • 0

So I have a dynamically allocated array of base class. I’ve stored a few objects of its derived class inside the array.

The student (base) class and its derived classes all have a getInfo() function, obviously the derived classes have overrided that base getInfo(). The goal is to used the getinfo function from the base class, then type class the two objects of the derived class, back into the derived class and use the overrided getinfo().

Everything up to “break” works perfect. It’s figuring out how to type cast the objects back into the derived classes that’s killing me.

I’ve identified a few possible issues:

1) I didnt dynamically allocate correctly. Very possible because I hate pointers and very much suck at them.

2) I have no idea what Im doing with regard to actually type casting.

A few things of note:

1) The base class getinfo is not virtual

2) I am not allowed to modify the base class.

So, saviors of confusing code. What say you? Can you help this poor student out?

EDIT!!!

Updated code, now getting “Static_cast from Student ** to Gradstudent * is not allowed”

#include <iostream>

#include "GradStudent.h"
#include "UndergradStudent.h"

int main()
{
    int arraySize = 3;
    Student* classRoom[arraySize];

    GradStudent gst1("Ta", "Da", 4444, 'A', "Death");
    cout << gst1;

    UndergradStudent ust1("Bluh", "Bluh", 2222, 1);
    cout << ust1;

    Student bst1( "Blah", "Blah", 1111 );

    classRoom[0] = &bst1;
    classRoom[1] = &gst1;
    classRoom[2] = &ust1;



    for (int x = 0; x < arraySize; x++)
    {
        cout << classRoom[x]->getInfo();
        cout << endl;
    }

    cout << "TEST" << endl;

    GradStudent* gStudent =  static_cast<GradStudent*>(&classRoom[2]);
    cout << gStudent->getInfo();



    return 0;
}
  • 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-19T01:52:29+00:00Added an answer on June 19, 2026 at 1:52 am

    dynamic_cast returns the casted pointer, it does not modify the parameter itself.

    You need to do something like below.

    // removed the & since the elements are now pointers
    GradStudent* gStudent =  dynamic_cast<GradStudent*>(classRoom[1]); 
    cout << gStudent->getInfo();
    

    Caveat – for pointers, if it fails to cast dynamic_cast will return nullptr, you need to check for this to prevent a crash.

    EDIT: Major Object slicing bug in your code – #1 is almost right

    int arraySize = 3;
    Student *classRoom;
    classRoom = new Student[arraySize];
    
    GradStudent gst1("Ta", "Da", 4444, 'A', "Death");
    ...
    classRoom[0] = gst1;
    

    Should be

    int arraySize = 3;
    Student **classRoom;
    //      ^
    classRoom = new Student*[arraySize];
    //                     ^
    GradStudent gst1("Ta", "Da", 4444, 'A', "Death");
    
    classRoom[0] = &gst1;
    //             ^
    

    Or just

    Student* classRoom[3];
    
    GradStudent gst1("Ta", "Da", 4444, 'A', "Death");
    ...
    classRoom[0] = gst1;
    

    Without this, all data belonging to the derived class is lost, and the base class data alone is stored, this is called Object Slicing.

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

Sidebar

Related Questions

I am trying to dynamically allocate an array of base(Student) classes, and then assign
I have a dynamically allocated array of polymorphic objects that I would like to
I have a class that contains a dynamically allocated array, say class A {
I have a class called set that is a dynamically allocated array in the
I have a 2 dimensional array dynamically allocated in my C code, in my
I am working with a std::vector to hold some objects that have dynamically allocated
I'm building a simple list using a dynamically allocated array in C++. I have
I'm writing a class that wraps a dynamically allocated array and I'm trying to
I have a dynamically allocated array : myRectangle lastRectanglesArray = new myRectangle[lastMaxLabel]; I would
I would like to extract unique values from my (dynamically allocated) array. I have

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.