With reference to
Method 1 and
Method 2 in main() my question is below:
Question: how does
1) instantiating an object on the heap, and then afterwards also allocating chunks of memory from the heap for the objects individual data members, compare to
2) Not instantiating on the heap, but still allocating chunks of memory from the heap for data members.
I understand how: Enemy* pBadGuy = new Boss(); works with respect to pointing pBadGuy to the new chunk of memory allocated for the Boss object, and then deleting the memory and setting the pointer to zero when the function ends, but I don’t understand how an instantiated new Boss as a whole object on the heap, compares (in terms of its existance on the heap as a whole object) to allocated chunks of memory from the heap for the individual data members.
Edit: I've been tempted to think if an object is instantiated on the heap, then why aren't all the data members automatically on the heap without having to assign them with "new". Also when applying delete to the pointer pBadGuy, why doesn't this destroy the whole object, Or does it. I'm confused!
Many Thanks, Gary.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
|
//Polymorphic Bad Guy
//Demonstrates calling member functions dynamically
#include <iostream>
using namespace std;
class Enemy
{
public:
Enemy(int damage = 10);
virtual ~Enemy();
void virtual Attack() const;
protected:
int* m_pDamage;
};
Enemy::Enemy(int damage) // Class Enemy
{
m_pDamage = new int(damage);
}
Enemy::~Enemy()
{
cout << "In Enemy destructor, deleting m_pDamage.\n";
delete m_pDamage;
m_pDamage = 0;
}
void Enemy::Attack() const
{
cout << "An enemy attacks and inflicts " << *m_pDamage << " damage points.";
}
class Boss : public Enemy // Class Boss
{
public:
Boss(int multiplier = 3);
virtual ~Boss();
void virtual Attack() const;
protected:
int* m_pMultiplier;
};
Boss::Boss(int multiplier)
{
m_pMultiplier = new int(multiplier);
}
Boss::~Boss()
{
cout << "In Boss destructor, deleting m_pMultiplier.\n";
delete m_pMultiplier;
m_pMultiplier = 0;
}
void Boss::Attack() const
{
cout << "A boss attacks and inflicts " << (*m_pDamage) * (*m_pMultiplier)
<< " damage points.";
}
int main()
{
// Method 1 (As Per Book)
cout << "Calling Attack() on Boss object through pointer to Enemy:\n";
Enemy* pBadGuy = new Boss(); // Instantiate On The Heap !!!
pBadGuy->Attack();
cout << "\nDeleting pointer to Enemy:\n";
delete pBadGuy;
pBadGuy = 0;
// Method 2 (My Test)
Boss test; // Not Instantiated On The Heap !!!
cout << "\nCalling Attack() on Boss object through pointer to Enemy:\n";
Enemy* pBadGuy2 = &test;
pBadGuy2->Attack();
cout << endl;
return 0;
}
|