Oh My God. It Just Hit Me.

This is a discussion on Oh My God. It Just Hit Me. within the C++ Programming forums, part of the General Programming Boards category; I now realize why pointers are so important. While sitting there for a while, trying to think of a way ...

  1. #1
    People Love Me
    Join Date
    Jan 2003
    Posts
    412

    Talking Oh My God. It Just Hit Me.

    I now realize why pointers are so important. While sitting there for a while, trying to think of a way to work out a problem, I thought of an idea.

    My problem:
    In my RPG, I wanted a different enemy object to be created each time you defeat one enemy. This would mean having to create many different objects. How on Earth would I make so many hundreds of functions without given myself a hear-attack, and at the cost of TONS of memory? OMG! Pointers! Yay!
    It's so obvious now:
    When a new enemy is created:
    Code:
    ...
    	Enemy Accessor;
    	int EHPX = Accessor.GetEHP(); // Not global, BTW.
    	int EATTX = Accessor.GetEAtt();
    	Enemy * BadGuy = new Enemy(EHPX,EATTX);
    ...
    Those are passed to the constructor:
    Code:
    Enemy::Enemy(int a, b)
    {
    	EHP = a;
    	EAtt = b;
    }
    Then, when the enemy is defeated:
    Code:
    ...
    	BadGuy->SetEHP(1); //strengthens enemy
    	BadGuy->SetEAtt(1); //strengthens enemy
    	delete BadGuy;
    ...
    Yay! I just use the object once, make it stronger after it dies, and then throw it away! When you encounter another enemy, another one is created, slightly stronger, and the process starts over.

    Yay for pointers!

  2. #2
    Registered User abrege's Avatar
    Join Date
    Nov 2002
    Posts
    369
    hurrah
    I am against the teaching of evolution in schools. I am also against widespread
    literacy and the refrigeration of food.

  3. #3
    Registered User rmullen3's Avatar
    Join Date
    Nov 2001
    Posts
    330

    ~

    Code:
    BadGuy->SetEHP(1); //strengthens enemy
    BadGuy->SetEAtt(1); //strengthens enemy
    delete BadGuy;
    Doesn't make sense... Unless SetEHP/SetEAtt modify stuff other than the stuff from the Enemy class, they're not of much use, considering BadGuy is thrown away in the next line anyway

    and I don't see why this problem would require pointers, and even more, dynamic memory- which is unsafe unless it's required- constructors are also called on things not created with new, and objects are usually destroyed whenever they go out of scope anyway.
    "He who makes a beast of himself, gets rid of the pain of being a man." Dr. Johnson

  4. #4
    People Love Me
    Join Date
    Jan 2003
    Posts
    412

    Re: ~

    Well, the code works, so that's all that matters. Yay for pointers!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. what race is god?
    By Leeman_s in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 02-22-2004, 05:38 PM
  2. GOD and religion
    By Unregistered in forum A Brief History of Cprogramming.com
    Replies: 19
    Last Post: 10-14-2001, 06:13 PM
  3. Foundations
    By mithrandir in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 10-05-2001, 03:18 PM
  4. God pong -update
    By gamegod3001 in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 09-03-2001, 10:16 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21