Hey everyone, I have been dealing with how to actually use this for a while now and it has been stopping my progress on a program I'm trying to make. I'm working on a game and I'm trying to figure out what I would need to allocate. I was thinking the object I create because in a class there are alot of data members declared there such as int, float, string ect.
So lets say I have a class called Hero. In the class I put what this hero will have:
example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
class Hero
{
protected:
int x;
int y;
int bx;
int by;
int HP;
int Energy;
ALLEGRO_BITMAP *hero1image;
//basically everything the hero will have including animation ect
}
|
and now in main of course after doing whatever is needed:
1 2 3 4 5
|
HeroImage = al_load_bitmap("Hero1.png"); // HeroImage was created using ALLEGRO _BITMAP*
Hero *hero = new Hero();
hero->InitObject(HeroImage); // A function to initialize the data members in the Hero class for hero and also passing an image so that I can store it to the image member part of the Hero class
|
Here would be what the InitObject(); does:
1 2 3 4 5 6 7 8 9
|
Hero::InitObject(ALLEGRO_BITMAP *Image)
{
x = 300;
y = 200;
//ect..
hero1image = Image;
}
|
so after I do all of this, do I have to delete the object in the destructor or do I need to make a function to delete the object later or do I delete the object hero in main after done using it? this is what confuses me is when do I delete it and if I passed the object to another function do I have to delete in that function or pass it back out of the function back to main?
Thanks if anyone can help me here cause I'm really confused and I want to pass object I allocate of the heap so I can manage to not use alot of memory. So basically I just want to know how do I pass objects around and when to delete the object or pretty much anything I allocate like variables storing images since they take memory.
also if anyone can suggest any other libraries or other tools to use to improve my programming that would be great, btw is Qt also a good program to use to get more better at programming? thanks again.