-
Question about classes
I'm trying to write a clone of Galaga using the allegro library and have been using classes. I have a class called enemy and I'm wondering why this isn't a valid call to the destructor
Code:
class Enemy {
int health;
public:
int x_position;
int y_position;
bool collision;
BITMAP *image;
//constructor
Enemy(int x, int y,int h = 1){
x_position = x;
y_position = y;
health = h;
bool collision=false;
image = load_bitmap("enemyShip.bmp", NULL);
}
//destructor
~Enemy(){destroy_bitmap(image);}
//class functions
int get_health(){return health;}
void movement(int&x,int&y);
};
then I have the function definiton for movement here
Code:
void Enemy::movement(int &x, int &y)
{
while(collision==false)
{
//try to add support for sin movement across screen
}
if (collision==true){~Enemy();}
}
is it not valid to call the destructor inside a class function?
this is the compiling error
error C2512: 'Enemy::Enemy' : no appropriate default constructor available
I'm figuring this means that it's thinking I'm trying to call a constructor? If so, am I mistaken that the ~Enemy() call isn't a destructor, I was under the impression that it was.
Sorry if this is a stupid question.
Thanks for the Help,
mjhamrick
-
I'm going to respond to your question with another question: does your code use placement new?
If you don't know what placement new is, or if your code does not use it, then you should NEVER explicitly call a destructor.
-
I can't say I know what placement new is, but my idea was that when the enemy got shot it would destruct the object? Now that I say that I guess I don't know if the destructor destructs the class, or an instance of an object. I might be trying something pointless here, I'm not sure. I just thought that might work for what I'm trying to do. The idea is that if collision true, then I could call the destructor and it would get rid of the instance of enemy and since I have in the destructor the code to destroy the bitmap, I figured that would get it off the screen. I haven't been coding in C++ that much, about a week, and I don't have much prior experience with Object oriented programming, so I might just be thinking about this the right way. Thanks for the response.
-
Your object does not know how to delete itself without making wild assumptions.
The object could exist on the stack or the heap, or could be part of an array etc. Those assumptions will get you into trouble.
If you want to have the object be deletable then you should enforce some conditions surrounding its creation. You can do this by making all constructors private and then providing a static creation method which allocates using new, and then your function can in theory call delete *this; This is still only safe if nothing attempts to access the object after it is deleted and so is usually only done in the context of an intrusive smart pointer, which is capable of NULLing itself.
Having said all that, why not just use a pre-made smart pointer which already safely does all of the above?
-
What exactly is a smart pointer?
-
Google shared_ptr. That is a smart pointer.