Question on Structure Destructor Over Ride and Dynamic Allocated Memory

Say, I have a structure like this
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
struct A
{
    short foo[1000];
    short fooo;
    bool ifDynamic;
    A(int i)
    {
        if (i>1000)
        {
            foo=new short [i];
            ifDynamic=1;
        }
        else ifDynamic=0;
        fooo=20;
    }
    ~A()
    {
        if(ifDynamic)    delete [] foo;
    }
}


And then I call the structure by A(3000), later destroy A with A.~A. I know the memory dynamically allocated to foo will be freed, but how about the original short[1000] allocated to foo? Will that be destroyed too? If not, how to destroy them? I don't want to create junk in my memory every time I call this structure...
Will that be destroyed too?

Yes, all members of the object are destroyed when the object is destroyed.

later destroy A with A.~A.

You don't call the destructor yourself. That's done by the implementation when the object is destroyed.
This:
short foo[1000];

and then this later:
foo=new short [i];
Should not even compile...
cannot convert from 'short *' to 'short [1000]'
And you forgot a semicolon! >:O
@clanmjc
well, actually I'm converting 'short[1000]' to 'short *' on certain circumstances... I thought basically they're a same thing...
foo needs to be a pointer to short if you are going to dynamically allocate like that. Otherwise, you just have an array of shorts on the stack.
Topic archived. No new replies allowed.