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?
1
Expert's answer
2010-08-04T11:10:56-0400
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!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS