+ 2
How do you destroy an object?
If you had: class test { public: test(int ii) : I(ii) {} int I; }; int main() { test Test; } What if I wanted to destroy Test? }
3 Respostas
+ 1
If you declare the variabile on the stack like you did, you can't manually delete it. It will get automatically deleted when the controll flow leaves the method you declared the object in.
in your example, the test object will be created (constructor call), then immediately destroyed (destructor call) since there are no other calls before the end of main.
If you want to manually destroy it, you must create it on the heap.
test* Test = new test;
Then delete it (destructor call)
delete Test;
0
delete Test;
- 1
1. ur Class name beginn with big letter: class Test{},
2. ~Test(); // This is the destructor: declaration