C++ Destructors | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

C++ Destructors

The 'delete' expression doesn't work with objects, right? Only with pointers to objects?

20th Dec 2017, 7:48 PM
Bennet
Bennet - avatar
3 Answers
+ 2
myClass * obj1 = new myClass(6); ... delete obj1;
20th Dec 2017, 9:42 PM
Вадим Сухотин (Vadim Sukhotin)
Вадим Сухотин (Vadim Sukhotin) - avatar
+ 2
Correct in the case of objects, delete removes the object from the pointer which takes it out of scope when using dynamically created objects. otherwise the objects naturally remove themselves from memory when they fall out of scope.
21st Dec 2017, 12:56 AM
Michael Simnitt
Michael Simnitt - avatar
+ 1
delete deletes those objects (or variables) for which the memory is dynamically allocated by the operator new. Of course, you can specify this object only with a pointer variable.
21st Dec 2017, 5:38 AM
Вадим Сухотин (Vadim Sukhotin)
Вадим Сухотин (Vadim Sukhotin) - avatar