Forum     

Go Back   Digit Technology Discussion Forum > Software > Programming
Register FAQ Calendar Mark Forums Read

Programming The destination for developers - C, C++, Java, Python and the lot


Closed Thread
 
LinkBack Thread Tools Display Modes
Old 13-08-2008, 06:15 PM   #1 (permalink)
Right Off the Assembly Line
 
Join Date: Dec 2007
Location: Nadiad ,Gujarat,India
Posts: 17
Question Destructor problem in C++


Hi Friends. I am Having some problems in running below program..
Please Help me..
Code:
#include<iostream>
using namespace std;
class mystring
{
    char* storage;
    int size;
    public: mystring() { storage=0;size=0; }
        mystring(char* s) 
        {
            size=strlen(s);
            storage=new char[size+1];
            strcpy(storage,s);
        }
        ~mystring() { cout<<"Deleting"<<endl;delete [](storage); }
        mystring operator + (mystring m)
        {
            char* temp=new char[size+m.size+1];
            strcpy(temp,storage);
            strcat(temp,m.storage);
            mystring t(temp);
            delete temp;
            return t;
        }
        void display()
        {
            cout<<storage;
        }
};

int main()
{
    mystring a1("Hello ");
    mystring a2("Anuj");
    cout<<"a1=";a1.display();cout<<endl;
    cout<<"a2=";a2.display();cout<<endl;
    mystring a3;
    a3=a1+a2;
    cout<<"a1=";a1.display();cout<<endl;
    cout<<"a2=";a2.display();cout<<endl;
    cout<<"a3=";a3.display();cout<<endl;
}
The output I get is as follows..
Quote:
anuj@edubuntu:05:55 PM:/media/ANUJ$ g++ -o mystring1.out mystring1.cpp
anuj@edubuntu:06:11 PM:/media/ANUJ$ ./mystring1.outa1=Hello
a2=Anuj
Deleting
Deleting
a1=Hello
a2=0�
a3= �o Anuj
Deleting
Deleting
Deleting
anuj@edubuntu:06:11 PM:/media/ANUJ$
I think the problem is the temp object(m), which deletes the original storage in "operator +" function ..(Please Tell me if I am wrong)
Can anyone suggest other alternatives for that??
Thanks in Advance.....
anuj919 is offline  
Advertisements. Register and be a member of the community to get rid of them.
Advertisement

Old 12-09-2008, 09:17 AM   #2 (permalink)
Right Off the Assembly Line
 
Join Date: Nov 2006
Posts: 48
Thumbs up Re: Destructor problem in C++

Hi,
in operator overloading function you are returning a local variable, which will be out of scope when this function returns. put that variable on heap and you will get the desire result.

if you are passing an object to some function make sure you pass it by reference.
else while doing inheritance it will create a problem.

if you are not changing argument inside the function pass as const reference.

I hope you understand.

Last edited by lilovirus; 21-09-2008 at 01:53 PM.
lilovirus is offline  
Old 15-09-2008, 08:06 AM   #3 (permalink)
Right Off the Assembly Line
 
Join Date: Dec 2007
Location: Nadiad ,Gujarat,India
Posts: 17
Smile Re: Destructor problem in C++

Thanks man
I got stuck with it for quite some time

Thanks again
anuj919 is offline  
Closed Thread

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem With Data Recovary Software Problem Stellar Phoenix njm Software Q&A 1 08-05-2007 12:31 PM
Problem Related To Dvd and Iso Buring , any genius here who can resolve this problem vijayendra QnA (read only) 2 14-01-2007 09:38 AM
Problem!Problem!Problem Help Me November Cd And December dvd Doesnt Work yyy??? sourav_digit QnA (read only) 3 27-12-2006 02:33 PM
Serius problem with my screen. Can u tell it s/w or hard ware problem . tusarks Hardware Q&A 7 12-06-2006 01:58 PM
fedora cd iso problem(installation from hard drive problem) harmax Open Source 3 01-07-2005 11:34 AM

 
Latest Threads
- by Sujeet
- by clmlbx
- by Sujeet
- by icebags

Advertisement




All times are GMT +5.5. The time now is 11:09 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.

Search Engine Optimization by vBSEO 3.3.2