Thread: Calling a destructor from inside the class

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    8

    Calling a destructor from inside the class

    how can i call a destructor from inside the class?
    i'm working on a game and i wan't to delete a monster once it reaches a certain location on the screen.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Well if your class contains

    monster *m;

    Birth is something like
    myvar->m = new monster;

    Then follows some moving around until the magic place is reached, at which point you do
    delete myvar->m;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    an object can call some cleanup code on itself, but should never destroy itself (and cannot, that i'm aware of). some other object should probably own or control the monster object, and should take care of deleting it.
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    It can certainly be done, particularly in the context of intrusive ref-counted objects.
    I wont be telling you how though until you can show what you are actually doing and that you really do need to do this after all. Otherwise I'd be loading a gun, cocking it, pointing it at your foot, and placing your finger on the trigger.

    More info please.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Usually, you need to control the lifetime of the object.
    If you have allocated the new object with new, use a smart pointer. Then make sure it goes of of scope when it you need it no longer. That's basics.
    Don't destroy the object internally.
    But it wouldn't hurt to elaborate a little.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Otherwise I'd be loading a gun, cocking it, pointing it at your foot, and placing your finger on the trigger.
    Hehe. Very nice analogy.

  7. #7
    Bored Programmer
    Join Date
    Jul 2009
    Location
    Tomball, TX
    Posts
    428
    Wouldn't this the be same condition as if you just outright killed the monster? I won't define them for you but these are the functions I use
    Code:
    if(!Monster.Dead())
      Monster.Handle();
    .................
    if(Monster.Collide(Bullet))
      Monster.Kill();
    just swap the Monster.Collide() with any condition you want and use a boolean to control the return value of Dead().
    and if I wanted to set his Death to trigger because of him moving to far.. I'd do so in his MoveBehavior() function.
    Last edited by Lesshardtofind; 09-12-2010 at 11:00 PM.

  8. #8
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    Monster.Kill would set the monster's death status. If you want to remove the monster object, there should be something like Game.RemoveMonster(aMonster).
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  9. #9
    Bored Programmer
    Join Date
    Jul 2009
    Location
    Tomball, TX
    Posts
    428
    Agreed but usually in a level with one monster I have a few like it and rather than create 7 different class declarations for the same monster I make one or two for each type and recycle them. Giving them different .Revive() conditions and then clearing them up after the level. So its not deleted until its not going to be potentialy used. This this a bad tactic?

  10. #10
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    if every monster of the same type is always in the same location, always has the same hp, always has the same [other stat], then what you're doing is fine, other than that you have to have separate instances in some manner. how many monsters on a level are we talking? if the monsters are going to be in and out like crazy, you could create a monster memory pool (allocate a pre-calculated/guessed number of monsters in advance). alternatively, you could try not deleting the monster until the level is deleted, but just have another boolean for the monsters - you already know if they're dead using isDead
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. deriving classes
    By l2u in forum C++ Programming
    Replies: 12
    Last Post: 01-15-2007, 05:01 PM
  2. Calling Base Class Operators
    By pianorain in forum C++ Programming
    Replies: 2
    Last Post: 03-10-2005, 10:53 AM
  3. Running Normal Functions inside of a class
    By Padawan in forum C++ Programming
    Replies: 2
    Last Post: 04-09-2004, 11:52 PM
  4. Calling constructor of the base class of a derived class..
    By CaptainPenguin in forum C++ Programming
    Replies: 5
    Last Post: 02-19-2003, 01:47 PM
  5. Pointer to class itself
    By Cdumbie in forum C++ Programming
    Replies: 3
    Last Post: 10-31-2002, 12:24 PM