Thread: Why Does The destructor destroy the original object??

  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をこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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
    28,413
    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).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

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, 02:56 PM