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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T14:09:28+00:00 2026-05-14T14:09:28+00:00

Suppose I have a class Dog that inherits from a class Animal . What

  • 0

Suppose I have a class Dog that inherits from a class Animal. What is the difference between these two lines of code?

    Animal *a = new Dog();
    Dog *d = new Dog();

In one, the pointer is for the base class, and in the other, the pointer is for the derived class. But when would this distinction become important? For polymorphism, either one would work exactly the same, right?

  • 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-14T14:09:29+00:00Added an answer on May 14, 2026 at 2:09 pm

    For all purposes of type-checking, the compiler treats a as if it could point to any Animal, even though you know it points to a Dog:

    • You can’t pass a to a function expecting a Dog*.
    • You can’t do a->fetchStick(), where fetchStick is a member function of Dog but not Animal.
    • Dog *d2 = dynamic_cast<Dog*>(d) is probably just a pointer copy on your compiler. Dog *d3 = dynamic_cast<Dog*>(a) probably isn’t (I’m speculating here, I’m not going to bother checking on any compiler. The point is: the compiler likely makes different assumptions about a and d when transforming code).
    • etc.

    You can call virtual functions (that is, the defined polymorphic interface) of Animal equally through either of them, with the same effect. Assuming Dog hasn’t hidden them, anyway (good point, JaredPar).

    For non-virtual functions which are defined in Animal, and also defined (overloaded) in Dog, calling that function via a is different from calling it via d.

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

Sidebar

Related Questions

Let's suppose I have a class Dog that inherits from class Animal, you might
Suppose we have declared these two classes: public class Animal { //..... } public
Suppose I have class animal and classes cat and dog extending it. I want
Suppose I have class D which inherits from class B . and I have
Suppose you have a class Dog , that has public class Dog { private
Suppose that I have two sets of associated types, for example Animal s and
Suppose I have a class that implements an interface: public class A implements IB
Suppose I have the following three classes: class Animal {}; class Human : public
Suppose I have: class A{ public $one; public $two; } and an array with
Suppose I have class Function , whose instances are callables that take one argument.

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.