+ 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? }

27th Nov 2016, 10:45 AM
Trey
3 Respuestas
+ 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;
30th Nov 2016, 11:18 AM
David Barzi
David Barzi - avatar
0
delete Test;
27th Nov 2016, 12:46 PM
Cohen Creber
Cohen Creber - avatar
- 1
1. ur Class name beginn with big letter: class Test{}, 2. ~Test(); // This is the destructor: declaration
27th Nov 2016, 11:00 AM
Ozan Karataş
Ozan Karataş - avatar