Re: constructor and destructor
From: red floyd (no.spam_at_here.dude)
Date: 10/22/03
- Next message: newtothis: "some assistance with a linked list"
- Previous message: dan: "how to count words from text"
- In reply to: sahukar praveen: "constructor and destructor"
- Next in thread: Karl Heinz Buchegger: "Re: constructor and destructor"
- Reply: Karl Heinz Buchegger: "Re: constructor and destructor"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Wed, 22 Oct 2003 08:01:07 GMT
sahukar praveen wrote:
> Hello,
>
> This is the program that I am trying. The program executes but does not give
> me a desired output.
>
> **********************************************
> #include <iostream.h>
> #include <iomanip.h>
> #include <string.h>
>
> class string{
> int length;
> char *str;
> public:
> string()
> {
> length = 0;
> str = new char [length + 1];
> strcpy(str, "\0");
> }
> string(const char *string)
> {
> cout<< "In the constructor" <<endl;
> length = strlen(string);
> str = new char [length + 1];
> strcpy (str, string);
> }
> void display (void)
> {
> cout <<"The length is : " << length<< endl;
> cout <<"The string is : " << str << endl;
> }
> ~string()
> {
> cout << "In the destructor " << length << endl;
> delete [] str;
> }
> };
>
> void main (void)
> {
> string A;
>
> A.display();
> A = string("Praveen"); //explicit call
> A.display();
> }
> ********************************************************
>
> The output that I get is
>
> The length is : 0
> The string is :
> In the constructor
> In the destructor 7
> The length is : 7
> The string is : иц
> In the destructor 7
>
>
> Now my question is
>
> 1. why is the "destructor 7" called twice? I would expect "destructor 0" to
> be called once and "destructor 7" called the next time. Am I correct?
> 2. I want to know is that line having a comment "explicit call" correct?
> Can I make a call like this?
>
> I would assume that my program has a bug (or is wrong) but I can not find
> out. Please Help.
>
1. main returns int, not void
2. I believe that the first destructor 7 call is for the temporary created by
string("Praveen")
3. You're getting garbage before the second destructor 7 because you don't have
an assignment operator.
What's happening is this:
* A is created length 0
* you display that.
* an unnamed temp is created length 7 containng "Praveen".
* You *BITWISE COPY* it to A (since you don't have an assignment operator),
meaning that A and your unnamed temp have the same value in str
* the unnamed temp is destroyed, destructor called, str is delete[] ed.
HOWEVER!!!! A.str still points to that delete[]'ed memory, and is not a valid
pointer now.
You display A, which puts out the eo garbage
You destroy A, which calls delete[] on an invalid pointe.
You need something like the following inside string:
string& operator=(const string& s)
{
if (str != s.str)
{
char *temp = new char[s.length + 1];
std::strcpy(temp,s.str);
delete[] str;
str = temp;
length = s.length;
}
return *this;
}
Your assignments will then work properly.
- Next message: newtothis: "some assistance with a linked list"
- Previous message: dan: "how to count words from text"
- In reply to: sahukar praveen: "constructor and destructor"
- Next in thread: Karl Heinz Buchegger: "Re: constructor and destructor"
- Reply: Karl Heinz Buchegger: "Re: constructor and destructor"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|