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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T05:04:20+00:00 2026-05-24T05:04:20+00:00

Possible Duplicate: Learning C++: polymorphism and slicing This is building off a question I

  • 0

Possible Duplicate:
Learning C++: polymorphism and slicing

This is building off a question I asked before.
The classes look like this:

class Enemy
{
    public:
        void sayHere()
        {
            cout<<"Here"<<endl;
        }
        virtual void attack()
        {
        }
};

class Monster: public Enemy
{

    public:
        void attack()
        {
            cout<<"RAWR"<<endl;
        }

};
class Ninja: public Enemy
{

    public:
        void attack()
        {

            cout<<"Hiya!"<<endl;
        }
};

I am new to C++ and I’m confused as to why this will only work with pointers (both Ninja and monster are derived from Enemy):

int main()
{
    Ninja ninja;
    Monster monster;

    Enemy *enemies[2];

    enemies[0] = &monster;
    enemies[1] = &ninja;

    for (int i = 0; i < 2; i++)
    {
        enemies[i]->attack();
    }

    return 0;
}

Why can’t I instead do this?:

int main()
{
    Ninja ninja;
    Monster monster;

    Enemy enemies[2];

    enemies[0] = monster;
    enemies[1] = ninja;

    for (int i = 0; i < 2; i++)
    {
        enemies[i].attack();
    }

    return 0;
}
  • 1 1 Answer
  • 2 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-24T05:04:21+00:00Added an answer on May 24, 2026 at 5:04 am

    This is a great question that hits at the heart of some of the trickier points of C++ inheritance. The confusion arises because of the difference between static types and dynamic types, as well as the way that C++ allocates storage for objects.

    To begin, let’s discuss the difference between static and dynamic types. Every object in C++ has a static type, which is the type of the object that is described in the source code. For example, if you try writing

    Base* b = new Derived;
    

    Then the static type of b is Base*, since in the source code that’s the type you declared for it. Similarly, if you write

    Base myBases[5];
    

    the static type of myBases is Base[5], an array of five Bases.

    The dynamic type of an object is the type that the object actually has at runtime. For example, if you write something like

    Base* b = new Derived;
    

    Then the dynamic type of b is Derived*, since it’s actually pointing at a Derived object.

    The distinction between static and dynamic types is important in C++ for two reasons:

    1. Assignments to objects are always based on the static type of the object, never the dynamic type.
    2. Invocations of virtual functions only dispatch to the dynamic type if the static type is a pointer or reference.

    Let’s address each of these in turn.

    First, one of the problems with the second version of the code is that you do the following:

    Ninja ninja;
    Monster monster;
    
    Enemy enemies[2];
    
    enemies[0] = monster;
    enemies[1] = ninja;
    

    Let’s trace through what happens here. This first creates a new Ninja and Monster object, then creates an array of Enemy objects, and finally assigns the enemies array the values of ninja and monster.

    The problem with this code is that when you write

    enemies[0] = monster;
    

    The static type of the lhs is Enemy and the static type of the rhs is Monster. When determining how to do an assignment, C++ only looks at the static types of the objects, never the dynamic types. This means that because enemies[0] is statically typed as an Enemy, it has to hold something precisely of type Enemy, never any derived type. This means that when you do the above assignment, C++ interprets this to mean “take the monster object, identify just the part of it that’s an Enemy, then copy that part over into enemies[0].” In other words, although a Monster is an Enemy with some extra additions, only the Enemy part of Monster will be copied over into enemies[0] with this line of code. This is called slicing, since you’re slicing off part of the object and leaving behind just the Enemy base portion.

    In the first piece of code that you posted, you have this:

    Ninja ninja;
    Monster monster;
    
    Enemy *enemies[2];
    
    enemies[0] = &monster;
    enemies[1] = &ninja;
    

    This is perfectly safe, because in this line of code:

    enemies[0] = &monster;
    

    The lhs has static type Enemy* and the rhs has type Monster*. C++ legally allows you to convert a pointer to a derived type into a pointer to a base type without any problems. As a result, the rhs monster pointer can be converted losslessly into the lhs type Enemy*, and so the top of the object isn’t sliced off.

    More generally, when assigning derived objects to base objects, you risk slicing the object. It is always safer and more preferable to store a pointer to the derived object in a pointer to a base object type, because no slicing will be performed.

    There’s a second point here as well. In C++, whenever you invoke a virtual function, the function is only called on the dynamic type of the object (the type of the object that the object really is at runtime) if the receiver is a pointer or reference type. That is, if you have the original code:

    Ninja ninja;
    Monster monster;
    
    Enemy enemies[2];
    
    enemies[0] = monster;
    enemies[1] = ninja;
    

    And write

    enemies[0].attack();
    

    then because enemies[0] has static type Enemy, the compiler won’t use dynamic dispatch to determine which version of the attack function to call. The reason for this is that if the static type of the object is Enemy, it always refers to an Enemy at runtime and nothing else. However, in the second version of the code:

    Ninja ninja;
    Monster monster;
    
    Enemy *enemies[2];
    
    enemies[0] = &monster;
    enemies[1] = &ninja;
    

    When you write

    enemies[0]->attack();
    

    Then because enemies[0] has static type Enemy*, it can point at either an Enemy or a subtype of Enemy. Consequently, C++ dispatches the function to the dynamic type of the object.

    Hope this helps!

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

Sidebar

Related Questions

Possible Duplicate: Should I learn C before learning C++? As a professional (Java) programmer
Possible Duplicate: Is it a good idea to learn JavaScript before learning jQuery? I
Possible Duplicate: Learning Python: If condition executing all the time This Python code is
Possible Duplicate: Wrapper class and == operator Saw this code in a website when
Possible Duplicate: SQL: What’s the difference between HAVING and WHERE? i am learning sql
Possible Duplicate: How do I calculate someone's age in C#? Maybe this could be
Possible Duplicate: Learning about LINQ Hi everyone, I just want to understand what exactly
Possible Duplicate: What does ‘unsigned temp:3’ means I'm learning some kernel code, and came
Possible Duplicate: How to stop C++ console application from exiting immediately? So im learning
Possible Duplicate: Char to int conversion in C. I remember learning in a course

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.