Thread: best way to destroy object?

  1. #1
    Shadow12345
    Guest

    best way to destroy object?

    can I just do
    this->~Name()
    to call the destructor
    or what ?

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    You can...but you dont want to in most circumstances....

    Either let the compiler call it, or have delete call it.....not yourself...

    Why would you want to do this?

  3. #3
    Shadow12345
    Guest
    oh well i forgot about delete.

    I just need to destroy an object once it goes off of the screen, that's all.

    Isn't it amazing that one can forget about something such as delete? I cannot believe this.

  4. #4
    Shadow12345
    Guest
    also I need to obtain the value of a variable that a class belongs to.

    in my case:
    a meteor has a particle, and I need the particle's position to be equal to the meteor's position that it belongs to minus another variable.

    Is there any way to do this 'automatically', or do I have to do this 'manually'

    note particle derives nothing from meteor, the only relationship they have is that particle is a member of meteor but they are two separate classes

    class particle{
    //crap}

    class meteor{
    //crap
    vector<Particle*>ParticleVector;
    }

    EDIT:
    Unless there is an easier way to do this ill just have to pass in parameters to particle::drawbody()
    Last edited by Shadow12345; 11-03-2002 at 10:11 AM.

  5. #5
    Code Monkey Davros's Avatar
    Join Date
    Jun 2002
    Posts
    812
    I think you want an owner relationship between your objects. I.e. do this something like this:

    Code:
    class Particle : public MyGameObject
    {
    private:
      MyGameObject* pOwner;
    public:
      Particle(MyGameObject* owner);
      MyGameObject* GetOwner() const;
    };
    So that the constructor sets the pOwner pointer on instantiation. pOwner points to your meteor object, of which Particle is now a part. Usually, the owning object is also responsible for deleting the objects it owns.

    Do you follow me?
    OS: Windows XP
    Compilers: MinGW (Code::Blocks), BCB 5

    BigAngryDog.com

  6. #6
    Shadow12345
    Guest
    yes that makes sense, i have something similar to that, i may post it when I am done. It is virtually the same except I don't use pointers or inheritance (i don't think there is any strong need for inheritance in my case)

    EDIT:
    here is what I am trying to do
    Code:
    #include "Includes.h"
    
    
    extern NumOfMeteors;
    
    class Particle {
    public:
    	Particle();
    	//POSITION OF PARTICLE, THIS WILL SLOW DOWN
    	float X;
    	float Y;
    	float Z;
    	//THE PARTICLE'S SPEED WILL DECREASE EITHER LINEARLLY OR EXPONENTIALLY
    	float XSpeed;
    	float YSpeed;
    	float ZSpeed;
    	//THE COLOR WILL START OFF AS RED, DECREASE COLOR BY THE SAME AMOUNT SPEED DECREASES
    	//WILL GO FROM RED TO BLUE
    	GLubyte Red;
    	GLubyte Green;
    	GLubyte Blue;
    	int DecreaseSpeed;	//THIS IS HOW 'FAST' IT 'SLOWS DOWN'
    	void DrawBody(float X, float Y, float Z);	//THESE PARAMS ARE THE POSITION OF THE METEOR
    private:
    
    };
    class Meteor{
    public:
    	Meteor();	//NEED TO CREATE A RANDOM STARTING POSITION, RANDOM NUMBER OF PARTICLES
    	~Meteor();
    	int NumOfParticles;
    	float X;
    	float Y;
    	float Z;
    	//SPEED SHOULD BE RANDOM BUT NO MORE THAN 4.0F
    	float XSpeed;	
    	float YSpeed;
    	float ZSpeed;
    	float Red;	//METEOR WILL ALWAYS HAVE STRONGER RED
    	float Green;
    	float Blue;
    	void DrawBody();
    	void DrawParticles();	//THIS WILL DRAW ALL OF THE PARTICLES THAT TRAIL THE METEOR
    	std::vector<Particle*>ParticleVector;	//CONTAINS ALL OF THE PARTICLES THAT RADIATE OFF OF IT
    	//static int NumMeteors;
    private:
    };
    Meteor:rawbody() is called from Main, and that in turn loops through all of the particles in the vector and calls their drawbody functions. Meteor passes in its own x y and z variables to particle::drawbody(float, float, float)
    Note I may not need the DrawParticles function


    umm...yeah!
    Last edited by Shadow12345; 11-03-2002 at 10:24 AM.

  7. #7
    Code Monkey Davros's Avatar
    Join Date
    Jun 2002
    Posts
    812
    >It is virtually the same except I don't use pointers

    I see what you're doing, and it all seems sensible. You don't have to use inheritence, that's just the way I would do it. However, you are indeed using pointers, look:

    std::vector<Particle*>ParticleVector;

    If you find later, that your particle object needs to refer to it's meteor owner (to find the meteor's position or something), then knowing who it's owner is would make this possible. May be worth keeping in mind, that's all I'm saying.
    OS: Windows XP
    Compilers: MinGW (Code::Blocks), BCB 5

    BigAngryDog.com

  8. #8
    Shadow12345
    Guest
    im going to continue writing it the way I am headed for right now, but I am going to post it, take suggestions to optimize it/make it 'better' and then re write it.

    How does that sound?

  9. #9
    Shadow12345
    Guest
    I'm trying to call delete but it isnt working

    I have tried:
    delete this;
    delete[] this;

    Neither works! What could I be doing wrong?

  10. #10
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    lol I dont know, I heard from somebody that that's a really really bad thing to do.

    But wait a minute, are you trying to remove an item from a std::vector? If you are, you call std::vector::erase() to get rid of it (but if it's a vector of pointers, you have to call delete on the pointer first, then call the vector's erase() to remove the pointer as well.)
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  11. #11
    Shadow12345
    Guest
    oh ok sweet

  12. #12
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    If you put it inside a set of scope operators, it will do the job too. For example,

    Code:
    {     vector <int> vintset;
          //do some stuff
    }    //vector will now be destroyed
    This won't call the delete operator for you though. So be careful.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Object destroy itself?
    By cminusminus in forum C++ Programming
    Replies: 28
    Last Post: 03-27-2008, 01:08 AM
  2. using this as synchronization object
    By George2 in forum C# Programming
    Replies: 0
    Last Post: 03-22-2008, 07:49 AM
  3. circular doubly linked list help
    By gunnerz in forum C++ Programming
    Replies: 5
    Last Post: 04-28-2007, 08:38 PM
  4. Question on l-values.
    By Hulag in forum C++ Programming
    Replies: 6
    Last Post: 10-13-2005, 04:33 PM
  5. A question about constructors...
    By Wolve in forum C++ Programming
    Replies: 9
    Last Post: 05-04-2005, 04:24 PM