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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T22:50:07+00:00 2026-06-06T22:50:07+00:00

I have a base class ( car ) and a class that inherit the

  • 0

I have a base class (car) and a class that inherit the base class (honda):

class car
{
    virtual void polymorphic_class()
    {   }
};

class honda : public car
{   };

When I use the following code and I cast my class a get a null pointer:

list<car> cars;
honda h;
cars.push_back(h);
honda* h_ptr = dynamic_cast<honda*>(&cars.back());
// h_ptr is NULL

Why? How I have to cast properly my object?

  • 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-06T22:50:08+00:00Added an answer on June 6, 2026 at 10:50 pm

    Polymorphism works on pointers and references, not on object instances.

    In this case, your list contains objects of type car, not of any derived type. When you insert a honda, it will copy the car part and ignore the rest; this is sometimes referred to as slicing.

    For polymorphism, you could use a list of pointers:

    list<car*> cars {new honda};
    honda * h_ptr = dynamic_cast<honda*>(cars.back()); // should be a valid pointer
    

    NOTE: If you do allocate using new as in my example, remember to either delete them, or store smart pointers (like std::unique_ptr<car>) rather than raw pointers. You’ll also need a virtual destructor in order to delete objects using a base-class pointer.

    You can avoid the slicing problem by making the base class abstract; if it contains pure virtual functions, then you can’t instantiate objects of that type, only of derived types that override those functions:

    class car
    {
        virtual ~car() {}
        virtual void do_something() = 0;
    };
    
    class honda : public car
    {
        void do_something() {}
    };
    

    If you don’t actually want an abstract interface (e.g. if you only access derived-class functionality using dynamic_cast rather than through virtual functions), then you could make the destructor pure virtual instead; then the derived classes won’t have to explicitly override anything. The base class destructor must still be implemented and, due to a quirk of the language, that implementation must be outside the class definition:

    class car
    {
        virtual ~car() = 0;
    };
    inline car::~car() {}
    
    class honda : public car {};
    

    This is a somewhat unusual approach, since polymorphism through virtual functions is usually more efficient and more convenient.

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

Sidebar

Related Questions

I have a model with the following associations: class Car < ActiveRecord::Base belongs_to :transportation_vehicle
I have a base class that has methods that use a generic type in
I have the following: class Car < ActiveRecord::Base has_one :driver end class Driver <
I have a model that looks like this - class Car < ActiveRecord::Base validates
I have STI implementation as follows: class Automobile < ActiveRecord::Base end class Car <
I have a base class, defined as below (I'm also using DevExpress components): public
I have a base class Vehicle and a derived class Car. package Game {
Suppose that we have the following base and derived classes: #include <string> #include <iostream>
I have these 3 models: class Car < ActiveRecord::Base belongs_to :user has_many :car_styles has_many
Let's say I have a Car class in a game that can be played

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.