F
fl
Hi,
I am learning C++ with C++ primer written by Lippman. The first output
line: cout << s1 << endl; works right. The second is wrong. I find the
problem is that s1 is destroyed in the call from "result += s;". And
even though the "result" is correct in the routine:
String
String:
perator+( const String &s ) const
{
String result = *this;
result += s;
return result;
}
It is wrong after callback. I am not sure where is the problem.
Because it is from the book example, maybe there is some little part I
do not notice. Thank you very much.
------------
#include <string.h>
#include <assert.h>
class String {
friend ostream&
operator<<( ostream&, String& );
public:
String( const char*);
String( int );
String operator+(const String &) const;
String& operator+=(const String &);
~String();
private:
int len;
char *str;
};
String::String( const char *s)
{
len = strlen( s );
str = new char[ len + 1 ];
assert( str != 0);
strcpy(str, s );
}
String::~String()
{ delete str;
}
ostream& operator<<( ostream& os, String& str )
{
char *s = str.str;
while ( *s ) os.put( *s++ );
return os;
}
String&
String:
perator+=( const String &s )
{
len += s.len;
char *p = new char[len+1];
assert( p != 0);
strcpy(p,str);
strcat(p,s.str);
delete str;
str=p;
return *this;
}
String
String:
perator+( const String &s ) const
{
String result = *this;
result += s;
return result;
}
-----------------------
#include <iostream.h>
#include "String.h"
main()
{
String s1("gobbledy");
String s2("gook");
s1 += s2;
cout << s1 << endl;
String s3 = s1 + s2;
cout << s3 << endl;
return 0;
}
I am learning C++ with C++ primer written by Lippman. The first output
line: cout << s1 << endl; works right. The second is wrong. I find the
problem is that s1 is destroyed in the call from "result += s;". And
even though the "result" is correct in the routine:
String
String:
{
String result = *this;
result += s;
return result;
}
It is wrong after callback. I am not sure where is the problem.
Because it is from the book example, maybe there is some little part I
do not notice. Thank you very much.
------------
#include <string.h>
#include <assert.h>
class String {
friend ostream&
operator<<( ostream&, String& );
public:
String( const char*);
String( int );
String operator+(const String &) const;
String& operator+=(const String &);
~String();
private:
int len;
char *str;
};
String::String( const char *s)
{
len = strlen( s );
str = new char[ len + 1 ];
assert( str != 0);
strcpy(str, s );
}
String::~String()
{ delete str;
}
ostream& operator<<( ostream& os, String& str )
{
char *s = str.str;
while ( *s ) os.put( *s++ );
return os;
}
String&
String:
{
len += s.len;
char *p = new char[len+1];
assert( p != 0);
strcpy(p,str);
strcat(p,s.str);
delete str;
str=p;
return *this;
}
String
String:
{
String result = *this;
result += s;
return result;
}
-----------------------
#include <iostream.h>
#include "String.h"
main()
{
String s1("gobbledy");
String s2("gook");
s1 += s2;
cout << s1 << endl;
String s3 = s1 + s2;
cout << s3 << endl;
return 0;
}