Hi I have created a small program which just adds two char* with the help of overloaded + operator, but getting problem with destructor.

#include<iostream>
#include<string.h>
using namespace std;
class base
{
        private:
                char* name;
        public:
                base();
                ~base();
                void display(void);
                base(const char*);
                base(const base&);
                base operator+(base foo);         
};

base::base()
{
        cout<<"base initialized"<<endl;
}

base::~base()
{
        cout<<name<<"destructor called"<<endl;
        delete name;

}

void base::display()
{
        cout<<"Name is: "<<name<<endl;
}

base::base(const base& foo)
{
        cout<<"copy called\n" ;
        name = new char(strlen(foo.name)+1);
        strcpy(name, foo.name);
}

base::base(const char* foo)
{
        name = new char(strlen(foo)+1);
        strcpy(name, foo);
}

base base::operator+(base foo)
{
        base temp;
        temp.name =  new char(strlen(name)+strlen(foo.name)+1);
        strcpy(temp.name,name);
        strcat(temp.name,foo.name);
        return temp;
}
 
int
main(int argc, char** argv)
{

base b1 = "alex";
base b2 = "parera";

base t1,t2,t3;

t1 = b1;
t2 = b2 ;
t3 = b1+b2;

b1.display();
b2.display();

t1.display();
t2.display();
t3.display();

return 0;

}

Here when program execution comes at t3 = b1+b2 point , it calls destructor and I am getting error, I can not see the concatenated string.. ie alexparera.

following is the output of the program

base initialized
base initialized
base initialized
copy called
base initialized
alexpareradestructor called
pareradestructor called
Name is: alex
Name is: parera
Name is: alex
Name is: parera
Name is:
destructor called
pareradestructor called
alexdestructor called
0�radestructor called
�destructor called

So My question is when I have not finished using t3 object in my program, y it calls the destructor and swaps out memory ??

You have three distinct problems.

1) You need to use square brackets instead of parentheses when dynamically allocating memory:

// Square brackets!
name = new char[strlen ( foo ) + 1];

2) You need to use the syntax for delete that matches the syntax for new:

base::~base()
{
  cout<<name<<"destructor called"<<endl;
  delete [] name;
}

3) You need to remember that assignment is different from copy construction. The following is all assignment:

t1 = b1;
t2 = b2 ;
t3 = b1+b2;

Yet you haven't defined an assignment operator. You can make it work by changing it to copy construction:

base t1 = b1;
base t2 = b2 ;
base t3 = b1+b2;

Thanx, got it now :))

It seems you have much more problems, for example:

base crash(0); // why not?

How about strlen(0) in your constructor?
Yet another example:

{
    base bomb;
    // Default constructor do not initialize name member!
    ...
} // Bang! Dectructor delete something via non-initialized pointer...

How to correct:

base::base(): name(0) // Now you print true message...
{
        cout<<"base initialized"<<endl;
}

Add name==0 test in display member function.

yea, and when writting the assignment operator=, don't forget the assignemt to self problem

Yes, I left these part intentionally as I was focused on the behaviour of destructor.

Thank you :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.