can I just do
this->~Name()
to call the destructor
or what ?
Printable View
can I just do
this->~Name()
to call the destructor
or what ?
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?
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.
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()
I think you want an owner relationship between your objects. I.e. do this something like this:
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.Code:class Particle : public MyGameObject
{
private:
MyGameObject* pOwner;
public:
Particle(MyGameObject* owner);
MyGameObject* GetOwner() const;
};
Do you follow me?
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
Meteor::Drawbody() 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)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:
};
Note I may not need the DrawParticles function
umm...yeah!
>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.
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?
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?
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.)
oh ok sweet
If you put it inside a set of scope operators, it will do the job too. For example,
This won't call the delete operator for you though. So be careful.Code:{ vector <int> vintset;
//do some stuff
} //vector will now be destroyed