On Jul 29, 3:31 pm, Juha Nieminen <nos...@thanks.invalid> wrote:
royash...@gmail.com wrote:
Was suggested in Effective C++ , In Case the Object contains a
Char* , a assignment operator = makes the two object point to the
same address . In this case any Of the two Object is destroyed a
memory leak occurs as one of the object stays on the Heap . My
experiments on the same have proved otherwise .
Perhaps you didn't understand correctly what Effective C++ was
trying to say.
If a class allocates memory for itself (at the end of a member
pointer) and frees that allocated memory in its destructor, it
requires a copy constructor and assignment operator if instances of
that class need to be copied and assigned. If there's no copy
constructor nor assignment operator and copying/assignment is
performed, it will cause double deletions of the same pointer,
as well as possible accessing of deleted memory.
Of course if the pointer inside the class points to something
not allocated by the class itself, something which the class itself
doesn't need to free but which is deleted somewhere else, then,
naturally, you don't need a copy constructor if it's ok for the
copies to point to the same memory location with that pointer.
Hi guys ,
i agree with you that double deletion shall happen as you say .
But when i tried it out , The string elements of the two objects
where pointing to two different memory locations . Thus the very
point of having a copy constructor fails . Even in the case of pass
by value , i found using VC++ , that the temp Objects that come into
exsistence have different addresses . Is this something that has
been corrected in the compiler ?
If you insist i shall again cross check my observations .
Regards ,
Ashish- Hide quoted text -
- Show quoted text -
I think you had better post a copy of the class your talking about.