Why Does The destructor destroy the original object??

This is a discussion on Why Does The destructor destroy the original object?? within the C++ Programming forums, part of the General Programming Boards category; The assignment operator will ALSO perform a plain memcpy(). Since that ALSO leads to two objects pointing to the same ...

  1. #31
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    The assignment operator will ALSO perform a plain memcpy(). Since that ALSO leads to two objects pointing to the same actual memory, it leads to similar problems.

    Obviously, the problem only happens when something attempts to change the contents of the pointer, or the pointer gets deallocated.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  2. #32
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,437
    Quote Originally Posted by chottachatri View Post
    OK this time i got it for sure!! you mean to say when we do call by value for objects(xstrlen..in this case) the default copy constructor get's called and it copies only the address of the original object and doesn't allocate the memory?right??
    Yes, the default copy constructor does a shallow copy. It simply copies the contents of the variables over to the new instance.

    and now what is anon talking about?why should be overload the assignment operator??the assignment operator is already overloaded isnt it??and i tried B=A as well as A=B but i am getting the right output only?
    Yes, you get right output, but try this:

    Code:
    void foo(str& mystr)
    {
    	str tempstr;
    	mystr = tempstr;
    	mystr.putdata();
    }
    
    int main()
    {
    	str str2;
    	str2.putdata();
    	foo(str2);
    	str2.putdata();
    	return 0;
    }
    Btw, this
    str str2;
    str2.putdata();
    Might actually crash your program. To avoid this, you would do well to create a constructor.
    Otherwise just add str2.getdata(""). Try this and you will encounter a little problem. The problem anon refers to.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #33
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,431
    you mean to say when we do call by value for objects(xstrlen..in this case) the default copy constructor get's called and it copies only the address of the original object and doesn't allocate the memory?
    No, it copies the member variables of the original object. If your original object has pointer member variables, it copies those too. But as it only copies the pointers, the objects that the pointers point to are not copied.

    why should be overload the assignment operator??the assignment operator is already overloaded isnt it?
    You need to define the copy assignment operator for the class to do deep copying as well (and destroy what's currently in the object).
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Page 3 of 3 FirstFirst 123
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Object destroy itself?
    By cminusminus in forum C++ Programming
    Replies: 28
    Last Post: 03-27-2008, 01:08 AM
  2. GDI object lifetime and C++ object lifetime
    By VirtualAce in forum Windows Programming
    Replies: 4
    Last Post: 06-16-2006, 05:26 AM
  3. Question on l-values.
    By Hulag in forum C++ Programming
    Replies: 6
    Last Post: 10-13-2005, 04:33 PM
  4. Set Classes
    By Nicknameguy in forum C++ Programming
    Replies: 13
    Last Post: 10-31-2002, 01:56 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21