Thread: base class pointer problems

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    465

    base class pointer problems

    this one has had me stumped for at least the past 2 hours.

    i am working on a simple genetic algorithm program with two types of creatures( predator and prey ) both derived from a base class creature.

    inside of my world manager class i have 2 pointer arrays, one holding pointers to predators and one holding pointers to prey.

    i also have a delete function that accepts a pointer to a creature ( the base class ) and deletes the pointer and sets it to NULL.

    whenever a creature dies, however, the pointer isnt completely deleted. i have tried changing the delete function to accept pointers to predators and preys instead of creatures but i get the same problem.

    my delete function is merely:
    Code:
    void Delete( Creature * creature )
    	{
    		delete creature;
    		creature = NULL;
    	}
    if you need any more code to solve the problem, let me know...
    I came up with a cool phrase to put down here, but i forgot it...

  2. #2
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Sounds more like artificial life than a GA.

    It would help to see more code, but I can guess what your problem is. Is your base class destructor virtual? If it isn't, then deleting a pointer to a creature (which is reall say a predator), will slice the class, deleting only the creature portion.

    Cheers
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  3. #3
    Registered User
    Join Date
    May 2003
    Posts
    161
    Code:
    void Delete( Creature * creature )
    {
      delete creature;
      creature = NULL;
    }
    When you set creature to NULL, it's only setting the local variable creature-- not the variable that you passed in. You can try passing a reference to a pointer:

    Code:
    void Delete(Creature*& creature)
    {
      delete creature;
      creature = NULL;
    }
    But that will only change the pointer that you've passed in the function call-- if you have other pointers to that particular creature elsewhere then it won't change them.

    -tf

  4. #4
    Registered User
    Join Date
    Feb 2002
    Posts
    465
    yeah, its an artificial life sim, but i use GA principles to do reproduction, etc...

    whether or not the classes get deleted isnt really a concern for me... there is nothing dynamic in them. the important thing was that the pointer get set to NULL.

    i just got rid of the function entirely and just replaced every function call with those two lines... :-\

    thanks anyway.
    I came up with a cool phrase to put down here, but i forgot it...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Replies: 25
    Last Post: 10-29-2007, 04:08 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Problems w.r.t Virtual Base Class (inheritance)
    By pankajdynamic in forum C++ Programming
    Replies: 1
    Last Post: 04-15-2002, 10:28 AM
  5. Difficulty superclassing EDIT window class
    By cDir in forum Windows Programming
    Replies: 7
    Last Post: 02-21-2002, 05:06 PM