In a class, there is a reference or pointer of an object of
another class embedded, and the memory is either allocated
or assigned to the new object created for this class. In
the constructor, parameters are passed to initialize the
data members and the embedded object reference to get
inialized. What measures or design change should be advised
for proper destruction and avioding memory leaks, getting
pointers dangling for the embedded object memory
allocation? Please suggest.

Answer Posted / som shekhar

If you are using the reference orpointer of other class as
an Aggregation (class own every thing, tight coupling) then
the destructor should take care of it, but if the reference
or pointer is taken as composition (class doesnt owwn the
pointer, loose coupling) then the class destructor should
not worry about that.

Is This Answer Correct ?    0 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Can we make copy constructor private in c++?

605


What is rvalue?

682


Why would you use pointers in c++?

630


On throwing an exception by the animal constructor in p = new animalq, can memory leak occur?

683


What is a map in c++?

587






What is the this pointer?

637


Will rust take over c++?

598


What is ofstream c++?

589


What can I safely assume about the initial values of variables which are not explicitly initialized?

627


What is an adjust field format flag?

646


What is the maximum value of a unsigned char a) 255 b) 256 c) 128

636


Write a function to find the nth item from the end of a linked list in a single pass.

571


Can manipulators fall in love?

569


Explain deep copy and a shallow copy?

616


Which is not a valid keyword a) public b) protected c) guarded

747