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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T23:22:27+00:00 2026-05-12T23:22:27+00:00

I am attempting to output functions common to a set of objects that share

  • 0

I am attempting to output functions common to a set of objects that share a base class and I am having some difficulty. When the objects are instantiated they are stored in an array and then I am attempting with the following code to execute functionality common to all the objects in this loop:

  if ( truck <= v ) // all types of trucks

vptr is an array of objects and the functions in the loop are common to all the objects. The code compiles fine but when I run it I get a segmentation fault when it enters this loop. I’m fairly confident that the call to the first function in this loop is what is causing the problem.

this is how I have instantiated the objects in a previous loop:

        vptr[ i ] = new Vehicle( sn, pc );

I should also mention, I’m sorry I forgot to be clear from the beginning, that in this array each object is of a different class. They all share a base class but they are derived objects of that class. Sorry for forgetting that probably important piece of information.

thanks
nmr

  • 1 1 Answer
  • 1 View
  • 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-12T23:22:28+00:00Added an answer on May 12, 2026 at 11:22 pm

    dynamic_cast to a pointer type returns a null pointer (aka 0, NULL) if the object isn’t of the specified type. You must check the pointer before using it, or use a reference type (which throws an exception on failure instead):

    if (Truck* p = dyanmic_cast<Truck*>(vptr[i])) {
      // use the pointer here
    }
    else {
      // vptr[i] doesn't point to a Truck
    }
    

    (Notice the nice effect of the correctly-typed pointer being scoped for you, take advantage of this when you can to improve readability.)

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

Sidebar

Related Questions

I am attempting to create an overloaded operator for a matrix class that I
I am attempting to redirect output of a Powershell script to a txt file.
I'm attempting to capture the output of fputcsv() in order to use gzwrite() to
I'm attempting to code a script that outputs each user and their group on
Attempting to print out a list of values from 2 different variables that are
Attempting to make a NSObject called 'Person' that will hold the login details for
I'm attempting to output audio to the bluetooth headset (not A2DP) using AVAudioPlayer, AVAudioSession
I'm attempting to write a library to do some domain-specific stuff. I'd like to
Here is my contrived example that illustrates what I am attempting to accomplish. I
Some background: I am attempting to create a DirectShow source filter based on the

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.