Element is a base class for Shape, which is a base class for Rectangles, Triangles, Circles, etc... This is part of a larger project I'm working on incorporating graphic programming. I'm just trying to eliminate any memory leaks early on. Also, this class *snipet* is a small part of the class. I didn't post the whole thing cause I only wanted the question about memory leaks addressed.
If you're curious (and perhaps have suggestions for improvement):
_parent helps track coordinates in relation to the window; needed for collision detection with buttons, combo boxes, etc.. For example, a button might be drawn onto an image, which is then drawn onto the window. Moving the image around never changes the coordinate position (_p) of the button in relation to the image, but it does change in relation to the window.. thus _parent keeps track of where in relation to the window the element is drawn.
1 2 3 4 5 6 7
|
/* Returns the position of this element relative to the window */
point element::getWindowPosition() const {
point tmp = _p;
if (getParent() != NULL) tmp += getParent()->getWindowPosition();
return tmp;
}
|
I'm not using vectors since an element doesn't require a parent, or it might have many parents. A button drawn directly to the window (has no parent) will return it's _p as its windowPosition(). However, if it's drawn with layers of parents (such as cascading windows) it will still return it's relative window position correctly. And also, no parent is assigned until the element is actually drawn, since until that time, you don't know if it's parent will be another element or the window directly.