Thread: Classes and Destroyed Instances

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    9

    Classes and Destroyed Instances

    If I created 5 instances of a class...

    IE:
    Code:
    class Enemy{blah blah}
    
    Enemy inst1;
    Enemy inst2;
    Enemy inst3;
    Enemy inst4;
    Enemy inst5;
    ...And I destroyed instances 1 3 and 4, but I use those instances in my coding...

    IE:
    Code:
    draw_object(inst1);
    draw_object(inst2);
    draw_object(inst3);
    draw_object(inst4);
    draw_object(inst5);
    ...how would I keep the program from executing functions using destroyed instances?

  2. #2
    Registered User
    Join Date
    Jul 2005
    Posts
    22
    If you use pointers you could set the objects to NULL when you destroy them, then check to see if an object is not NULL before you use it. Or in your deconstructor you could set a flag like bDead = true; and check the value of bDead before you use an object.

  3. #3
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    if you're not using pointers, the only way to really destroy an object is to let it run out of scope. in that case, you'd get a compile error when you hit the statements with them in it. if you're talking dynamic memory, you're talking new/delete, and this is how you could do it (essentially what Sfpiano just told you about the pointers):
    Code:
    #include<iostream>
    
    struct test     //I'm using this in place of your classes
    {
            int i;  //just some dummy data
    };
    
    int main()
    {
            test a;                 //instantiate test
            test*b=new test;        //create a test pointer, and initilize it
            a.i=0;                  //fill in the dummy data for a
            b->i=1;                 //fill in the dummy data for b
            delete b;               //delete b
            b=0;                    //make the pointer point to nothing (NULL)
    
            std::cout<<a.i;         //output a
            if(b!=0)                //check if b pointer is pointing to a test
            {
                    std::cout<<'\n'<<b->i;  //if it is, print it
            }
            std::cout<<std::endl;   //either way put a newline & flush the stream
            return 0;               //return 0
    }
    Last edited by major_small; 07-12-2005 at 09:57 PM.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  4. #4
    Registered User
    Join Date
    Jul 2005
    Posts
    9

    Thanks

    Thanks for the help...I thought of something similar to that (it involved pointers anywayz) but was much more complicated. So thanks again!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Classes that can crash during initialization
    By drrngrvy in forum C++ Programming
    Replies: 23
    Last Post: 06-27-2006, 06:13 PM
  2. Help with Class Library .NET and Classes :0
    By Robert_Sitter in forum Windows Programming
    Replies: 2
    Last Post: 11-27-2005, 08:42 PM
  3. Destroying a vector of classes
    By TheSquid in forum C++ Programming
    Replies: 6
    Last Post: 02-24-2005, 08:26 PM
  4. Classes, dynamic variables, and leaks.
    By Grins2Pain in forum C++ Programming
    Replies: 8
    Last Post: 09-26-2003, 03:07 PM