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

  1. #16
    Registered User
    Join Date
    Jan 2008
    Posts
    225
    Ok is this one now correct??

    Code:
    class str
    {
    	char *s;
    	public:
    		str(){}
    		~str()
    		{
    			delete []s;
    		}
    		str(str &p)
    		{
    			s=new char[strlen(p.s)+1];
    			strcpy(s,p.s);
    		}
    		void getdata(char*);
    		void putdata();
    		friend int xstrlen(str);
    
    };

  2. #17
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Looks good to me.

    --
    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.

  3. #18
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yep, much better. You can try to use more descriptive names for your variables and rename getdata/putdata to more appropriate names, too, if you want something extra to do.
    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.

  4. #19
    Registered User
    Join Date
    Jan 2008
    Posts
    225
    Ok last and final question..is what i am doing in this function is that i am allocating a new memory space for my string so that the "string" and *s point to different memory location so when the destructor frees the memory the original object doesn't get destroyed?
    is this correct what i have understood?

  5. #20
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yes, you are correct. You're making a copy of the data, so each class has its own instance of the string.
    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.

  6. #21
    Registered User
    Join Date
    Jan 2008
    Posts
    225
    Ok this is deep copying. Now what is shallow copying?that one which is there in getdata???right??

    but in that also i allocated the memory so how come *s and "string" both point to the same location?

  7. #22
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by chottachatri View Post
    Ok this is deep copying. Now what is shallow copying?that one which is there in getdata???right??
    Shallow copy means you're just copying pointers over, while deep copy means you copy the data pointed to by the pointers, as well.

    but in that also i allocated the memory so how come *s and "string" both point to the same location?
    Huh? There is no "string." You forgot to give them a descriptive name.


    Oh, I also see something else. Always put names for arguments in class definitions, as well.
    For example:
    void getdata(char* pStr);
    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.

  8. #23
    Registered User
    Join Date
    Jan 2008
    Posts
    225
    You mean the same 2 lines i have written in getdata and in copy constructor but still 1 is deep copy and 1 is shallow copy???

    You mean the difference is just of the copy constructor??is it the root?

  9. #24
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by chottachatri View Post
    You mean the same 2 lines i have written in getdata and in copy constructor but still 1 is deep copy and 1 is shallow copy???

    You mean the difference is just of the copy constructor??is it the root?
    deep and shallow copying refers to how you copy an object. getdata() is not copying an object, is it?

    --
    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.

  10. #25
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by chottachatri View Post
    You mean the same 2 lines i have written in getdata and in copy constructor but still 1 is deep copy and 1 is shallow copy???
    No, both are deep copy. Look again.

    You mean the difference is just of the copy constructor??is it the root?
    That makes no sense...
    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.

  11. #26
    Registered User
    Join Date
    Jan 2008
    Posts
    225
    If both are deep copy then Elysia how come are they(object's pointer and the string) pointing to the same memory location?

  12. #27
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    They won't, unless you do something wrong.
    Remember that there are two instances of the objects. Both of these two will not pointer to the same memory location, since you allocated more memory and copied the data over.
    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.

  13. #28
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Now you'll also need to overload the assignment operator. Otherwise you'll have the original problem when you do this:
    Code:
    str a, b;
    a = b;
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  14. #29
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by chottachatri View Post
    If both are deep copy then Elysia how come are they(object's pointer and the string) pointing to the same memory location?
    Huh?

    if you call the putData() function, you get a copy of the string [which your existing code is not freeing], and the copy constructor creates a copy of the string. So neither is pointing to the string in the original object.

    --
    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.

  15. #30
    Registered User
    Join Date
    Jan 2008
    Posts
    225
    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??

    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?

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