Deleting contents of an Object

Hello,
I am a complete newbie to cpp..so pls forgive if i ask something obvious.

say for eg. i have a class Temp:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Temp
{
   public:
          Temp();
          ~Temp();   
          char* value;   
};

Temp::Temp()
{
     value="hello";
}

Temp::~Temp()
{
   delete value;
}



and then i have another class AnotherTemp :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include "Temp.cpp"

class AnotherTemp
{
    public:
            void MyFunction();
}

AnotherTemp::MyFunction()
{
    Temp t; 
    Temp* tt=new Temp;
    delete tt;
}



Here i make 2 Objects of Temp class in MyFunction().

when declaring t does out of scope destructor of Temp class will be called and hence my char* value will also be gone.

But when i do statement delete tt, will char* value be also gone.

In such a case will destructor be called.

help appreciated
amal
What are you asking?

AnotherTemp::MyFunction() will attempt to run the Temp destructor twice... once for t and once for *tt. However, your Temp destructor is wrong, because value is initialized from a constant string in the constructor and you will corrupt the heap attempting to delete value.
what i was asking was will the char* value inside Temp class would be deleted when i do delete tt.


"However, your Temp destructor is wrong, because value is initialized from a constant string in the constructor and you will corrupt the heap attempting to delete value."


ok..so in such a case delete value; statement inside the Destructor is not required.


help appreciated
amal
Topic archived. No new replies allowed.