Struct and Class Instances

I have a problem, I wanted to auto setup the structure members by calling a function. I had this structure:

1
2
3
4
5
6
struct hgeQuad
{
  hgeVertex  v[4];
  HTEXTURE   tex;
  int        blend;
};


Then I created this class as an instance of the structure above.

1
2
3
4
5
6
class hgeBox:hgeQuad
{
	public:
		hgeBox(float x, float y, float size_x, float size_y,DWORD col);
		~hgeBox();
};


box = new hgeBox(16,18,160,12,0xFF00FF00);

The problem is: How can I free the memory I allocated?

would it be just

delete box?
I think that actually depends on the type of box. If box is a actually of type *hgeQuad then you need to include a virtual destructor for hgeQuad. Check out this link: http://www.parashift.com/c++-faq-lite/virtual-functions.html#faq-20.7
Yes, it's 'delete box'. unless there is some other freeing to do (like deleting the texture or something) then you should write a destructor.
Thank you very much for the answers, that really helped. I'd also like to add that

hgeVertex is another structure, and HTEXTURE is just a pointer.
struct hgeVertex
1
2
3
4
5
6
{
  float  x, y;
  float  z;
  DWORD  col;
  float  tx, ty;
};


I did as slicedpan told me, I wrote this destructor and got no errors so far.

1
2
3
4
5
6
hgeBox::~hgeBox()
{
	for(int i=0;i<4;i++)
	    free(&v[i]);
	free(this);
}


I'm not sure if this is the way I shouls use this "this", is it correct to free(this)?
i think you have a few misconceptions about garbage collection in c++, free should only be used if you used malloc to allocate memory. Likewise delete should only be used if you used new to allocate.

so for example if i have a class like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class myClass
{
public:
    int my_i;
    char my_ch;
    double my_d;
    std::string * my_str;
    myClass();
    ~myClass();
};

myClass::myClass()
{
    my_str = new std::string("this is a string");
}

myClass::~myClass()
{
    delete my_str;        //this is the only member that has to be explicitly deleted
}


So for example let's say the main() function creates a myClass object like so:

1
2
3
4
5
6
myClass * mc_ptr;
mc_ptr = new myClass();

//do some stuff here

delete mc_ptr;


when i call delete mc_ptr; the destructor for myClass is called. this deletes the string that is allocated when the constructor was called. the other variables (my_i, my_ch, my_d) are automatically deallocated at this time.
All right, I guess I got it now. Thank you all.
Topic archived. No new replies allowed.