Answer to Question #534 in C++ for Albert

Question #534
Is it possible for dynamically allocated objects to be automatically deleted when you exit out of scope?
Expert's answer
Technically, it is possible, but you will have to use tricks. You have to create a class that will encapsulate the dynamic object. In constructor you allocate memory for it, and in destructor you free it. If this object is non-trivial class and you want to be able to use the methods of this class via the class member's selector, you can overload the operator “->” and make sure that it returns an encapsulated pointer.

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!


No comments. Be first!

Leave a comment

Ask Your question

New on Blog