clase Surfaces
{
LPDIRECTDRAWSURFACE lpPrimary;
LPDIRECTDRAWSURFACE lpBackbuffer;
Surfaces(){};
~Surfaces(){};
};
Surfaces::Surfaces()
{
lpPrimary = NULL;
lpBackbuffer = NULL;
}
Surfaces::~Surfaces()
{
if(lpBackbuffer) lpBackbuffer->Release();
if(lpPrimary) lpPrimary->Release();
class DDObj
{
LPDIRECTDRAW lpDD;
DDObj(){};
DDObj(){};
};
DDObj::DDObj()
{
lpDD = NULL;
}
DDObj::~DDObj()
{
if(lpDD) lpDD->Release();
}
//The main program
Surfaces mySurfaces;
DDObj myDD;
now when the program ends, they too classes goes out of scope and their destructors are called. Which are first? I wouldn''t want to release the DirectDraw Object before the Surfaces right?
Out of Scope Precedence
Let''s say I have too classes, they are dumb and serve for demo propose only:
I don''t really know the answer, but I think you cannot know beforehand which will be deleted first. But there are two ways to solve this.
First, you could use pointers like this:
Surfaces* mySurfaces = new Surfaces;
DDObj* myDD = new DDObj;
and at the end of your program, you just delete them in the order you want to:
delete mySurfaces;
delete myDD;
And second, why won''t you just move the two classes into one larger class containing your DirectDraw object and surfaces together?
First, you could use pointers like this:
Surfaces* mySurfaces = new Surfaces;
DDObj* myDD = new DDObj;
and at the end of your program, you just delete them in the order you want to:
delete mySurfaces;
delete myDD;
And second, why won''t you just move the two classes into one larger class containing your DirectDraw object and surfaces together?
Things get destroyed in the reverse order they are constructed.
If in your main code, you have this:
DDObj myDD;
Surfaces mySurfaces;
The DDObj will get constructed first and destroyed last. Of course if you have them at global level in different files, you can''t know which order they''ll get destroyed.
I hope this helps,
- Pete
If in your main code, you have this:
DDObj myDD;
Surfaces mySurfaces;
The DDObj will get constructed first and destroyed last. Of course if you have them at global level in different files, you can''t know which order they''ll get destroyed.
I hope this helps,
- Pete
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement