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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T05:15:36+00:00 2026-05-14T05:15:36+00:00

I have an array of custom class Student objects. CourseStudent and ResearchStudent both inherit

  • 0

I have an array of custom class Student objects. CourseStudent and ResearchStudent both inherit from Student, and all the instances of Student are one or the other of these.

I have a function to go through the array, determine the subtype of each Student, then call subtype-specific member functions on them.

The problem is, because these functions are not overloaded, they are not found in Student, so the compiler kicks up a fuss.

If I have a pointer to Student, is there a way to get a pointer to the subtype of that Student? Would I need to make some sort of fake cast here to get around the compile-time error?

  • 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-05-14T05:15:37+00:00Added an answer on May 14, 2026 at 5:15 am

    You need a dynamic cast:

    Student * s = new ...;    // Create student of some sort.
    
    if ( ResearchStudent * r = dynamic_cast<ReasearchStudent*>( s ) ) {
       r->ResFunc();
    }
    else if ( CourseStudent * c = dynamic_cast<CourseStudent*>( s ) ) {
       c->CourseFunc();
    }
    else {
       throw "Unknown student type.";
    }
    

    Note that this uses type information maintained by the compiler, provided the class has at least one virtual function – if all else fails, make the destructor virtual (as it must be in this case anyway). You should always prefer this approach to maintaining your own type information.

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

Sidebar

Related Questions

I have an array filled with instances of a custom class which contains two
I have a method that returns an array of custom class objects that are
Let's say I have an array of custom objects. The custom class looks like
I have a NSArrayController bound to NSUserDefaultsController, and the array stores a custom class
I have a collection of custom objects (Podcast) in an array. When I use
I have a listview with a custom array adapter: public class SmsMessageAdapter extends ArrayAdapter<ContactMessage>
I have an array in Perl: my @my_array = (one,two,three,two,three); How do I remove
Is it possible to subclass and inherit from javascript Arrays? I'd like to have
I have a custom made class called Graph in which I use adjacency lists.
[[NSUserDefaults standardUserDefaults] setObject:myArray forKey:@myArray]; If I make an array of custom objects conform to

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.