0

I've been taking CodingMadeEasy's C++ Made Easy HD tutorials (If you'r not familiar with him, he's a youtuber that gives amazing lessons in multiple programming languages and libraries), and lately I've been taking his lesson on Polymorphism as he's teaching us object oriented programming. I've been understanding all the stuff he's been teaching us, but I've gotten rather stuck where he decides to call a method from the "Player" class without an instance of the class. Only one line of code bugs me and in turn causes me to misunderstand the rest of the example. The line of code is:

class Entity
{
protected:
    int atkPow;

public:
    void SetAtkPower(int value)
    {
        atkPow = value;
    }
};

class Player : public Entity
{
public:
    void Attack()
    {
        cout << "Player Attack: " << atkPow << endl;
    }
};

int main()
{
    Entity *entity = new Player; //THIS IS THE LINE OF CODE THAT I CAN'T UNDERSTAND
    entity->SetAtkPower(10);

    delete entity;
}

Now as far as I'm concerned, I should understand all dynamic memory has to offer, but how is an instance of the Entity class pointing to new Player?

Here is the CodingMadeEasy tutorial. The bit in which I fail to understand start 8 minutes in.

If somebody could clarify things for me, I'd greatly appreciate it. Thank you in advance :)

  • 1
    Use smart pointers instead of managing your own memory. – chris Aug 12 '13 at 01:03
  • @chris He isn't asking about memory management. Shouldn't he learn the basics before advanced shiz? – Lews Therin Aug 12 '13 at 01:05
  • @LewsTherin, How is it advanced (moreso than polymorphism)? It's practically the same interface, but is inherently much safer. Anyway, it's an improvement that can be applied beyond this specific question. – chris Aug 12 '13 at 01:06
  • @chris Advanced clearly because he doesn't understand how inheritance and polymorphism are linked. That's like learning about pass by pointer value before learning about pass by value. – Lews Therin Aug 12 '13 at 01:07
  • @chris Anyway, I don't want to get into an argument. I just think his question should be answered first, then PS the advice. – Lews Therin Aug 12 '13 at 01:08
  • @LewsTherin, Then I'll leave this right where it is. – chris Aug 12 '13 at 01:09
  • `new Player` creates a new `Player` object on the heap. `entity` is assigned the address to this new object. So `entity` acts like a handle to this object instance. Which part of this is unclear? – greatwolf Aug 12 '13 at 01:20
  • 2
    Such a simple program and yet UB. – dyp Aug 12 '13 at 01:22
  • @DyP Check if I'm interpreting this right. There's nothing in there that makes the class designated as a 'virtual' anything. The destructor called on delete will be the base class destructor and then there's UB because nothing happened with the derived class' destructor. And all of this could've been fixed with a `virtual ~Entity()` decldef. – user Aug 12 '13 at 01:25
  • @Atash yeah, that's about it. Actually, the C++ Standard says in [expr.delete]/3 "[...] if the static type of the object [read: expression] to be deleted is different from its dynamic type, the static type shall be a base class of the dynamic type of the object to be deleted **and the static type shall have a virtual destructor or the behavior is undefined.**" – dyp Aug 12 '13 at 01:28
  • IMHO you should take a look at the community-created [Definitive C++ Book Guide and List](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). This is a very basic question, and IMHO the video tutorial isn't very good: the program contains UB, implementation-defined behaviour (`system("pause");`), doesn't promote modern C++ (smart pointers) and contains an example / explanation of OOP and `virtual` that is confusing. Quoting the video @ 11:33 "but if we add `virtual` in there, then it will call the function from which [erm] we're pointing to" – dyp Aug 12 '13 at 02:04

1 Answers1

0

You declared that a Player is an Entity.

The line you point to means "Allocate a new Player. Let the variable entity which is an Entity pointer, point to the newly allocated Player. entity can point to any entity, and as Player is an Entity it can also point to a Player.

Kornel Kisielewicz
  • 55,802
  • 15
  • 111
  • 149