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::operator+( 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::operator+=( 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::operator+( 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;
} 7 1271
On 2007-12-28 11:56:02 -0500, fl <rx*****@gmail.comsaid:
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::operator+( 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::operator+=( 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::operator+( 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;
What do you think the [default] copy constructor is doing in the line above?
cout << s3 << endl;
return 0;
}
--
-kira
On 28 déc, 12:29, Kira Yamato <kira...@earthlink.netwrote:
On 2007-12-28 11:56:02 -0500, fl <rxjw...@gmail.comsaid:
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::operator+( 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::operator+=( 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::operator+( 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;
What do you think the [default] copy constructor is doing in the line above?
* *cout << s3 << endl;
* *return 0;
}
--
-kira- Masquer le texte des messages précédents -
- Afficher le texte des messages précédents -
You mean this:
s1 += s2;
cout << s1 << endl;
It corrects. But the book later propose the s3=s1+s2 as a new
concatenation method. From the context, it implies they can work
together (It says it uses the += operator of the previous definition).
Thanks.
fl wrote:
>What do you think the [default] copy constructor is doing in the line above?
cout << s3 << endl;
return 0;
}
--
-kira- Masquer le texte des messages précédents -
- Afficher le texte des messages précédents -
You mean this:
s1 += s2;
cout << s1 << endl;
It corrects. But the book later propose the s3=s1+s2 as a new
concatenation method.
But you did not write that. You wrote:
String s3 = s1 + s2;
From the context, it implies they can work together (It says it uses the
+= operator of the previous definition).
Thanks.
Yes, but the line you wrote in your program uses the copy constructor. Since
you didn't define one, the compiler generates one for you, but in this
case, it doesn't do what you want.
The "Rule of Three" applies: If you need to write one of copy constructor,
copy assignment operator and destructor, you can be pretty certain that you
need all three of them.
On 28 déc, 13:45, Rolf Magnus <ramag...@t-online.dewrote:
fl wrote:
What do you think the [default] copy constructor is doing in the line
above?
cout << s3 << endl;
return 0;
}
--
-kira- Masquer le texte des messages précédents -
- Afficher le texte des messages précédents -
You mean this:
* s1 += s2;
* cout << s1 << endl;
It corrects. But the book later propose the s3=s1+s2 as a new
concatenation method.
But you did not write that. You wrote:
String s3 = s1 + s2;
From the context, it implies they can work together (It says it uses the
+= operator of the previous definition).
Thanks.
Yes, but the line you wrote in your program uses the copy
constructor.
Thanks. Because it is new to me, I would like to be clear with what
you mean. The follow is my new copy constructor? I see the follow is
called.
String
String::operator+( const String &s ) const
{
String result = *this;
result += s;
return result;
}
And I have to write the respective assignment operator and destructor?
Since
you didn't define one, the compiler generates one for you, but in this
case, it doesn't do what you want.
The "Rule of Three" applies: If you need to write one of copy constructor,
copy assignment operator and destructor, you can be pretty certain that you
need all three of them.- Masquer le texte des messages précédents -
- Afficher le texte des messages précédents -
On 28 déc, 13:45, Rolf Magnus <ramag...@t-online.dewrote:
fl wrote:
What do you think the [default] copy constructor is doing in the line
above?
cout << s3 << endl;
return 0;
}
--
-kira- Masquer le texte des messages précédents -
- Afficher le texte des messages précédents -
You mean this:
* s1 += s2;
* cout << s1 << endl;
It corrects. But the book later propose the s3=s1+s2 as a new
concatenation method.
But you did not write that. You wrote:
String s3 = s1 + s2;
From the context, it implies they can work together (It says it uses the
+= operator of the previous definition).
Thanks.
Yes, but the line you wrote in your program uses the copy constructor. Since
you didn't define one, the compiler generates one for you, but in this
case, it doesn't do what you want.
The "Rule of Three" applies: If you need to write one of copy constructor,
copy assignment operator and destructor, you can be pretty certain that you
need all three of them.- Masquer le texte des messages précédents -
- Afficher le texte des messages précédents -
Thanks to both of you. I solve it now.
On Fri, 28 Dec 2007 08:56:02 -0800, fl wrote:
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::operator+( 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& );
Are you sure the operator is going to change the String argument?
public:
String( const char*);
String( int );
String operator+(const String &) const; String& operator+=(const
String
&);
~String();
private:
int len;
char *str;
};
You forgot about copy constructor (as Kira Yamato noticed)
public:
String( const String & rhs);
>
String::String( const char *s)
{
len = strlen( s );
str = new char[ len + 1 ];
assert( str != 0);
This will never happen. If new fails, bad_alloc is thrown.
strcpy(str, s );
}
String::~String()
{ delete str;
}
Undefined behaviour. It should be
delete[] str;
>
ostream& operator<<( ostream& os, String& str ) {
char *s = str.str;
while ( *s ) os.put( *s++ );
return os;
}
You don't change the str argument, so pass a const reference.
And code
os << str.str;
return os;
seems better to me
>
String&
String::operator+=( 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::operator+( 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;
}
--
Tadeusz B. Kopec (tk****@NOSPAMPLEASElife.pl)
A day for firm decisions!!!!! Or is it?
On 28 déc, 15:53, "Tadeusz B. Kopec" <tko...@NOSPAMPLEASElife.pl>
wrote:
On Fri, 28 Dec 2007 08:56:02 -0800, fl wrote:
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::operator+( 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& );
Are you sure the operator is going to change the String argument?
public:
* *String( const char*);
* *String( int );
* *String operator+(const String &) const; String& operator+=(const
String
* *&);
* *~String();
private:
* *int len;
* *char *str;
};
You forgot about copy constructor (as Kira Yamato noticed)
public:
* String( const String & rhs);
String::String( const char *s)
{
* *len = strlen( s );
* *str = new char[ len + 1 ];
* *assert( str != 0);
This will never happen. If new fails, bad_alloc is thrown.
* *strcpy(str, s );
}
String::~String()
{ *delete str;
}
Undefined behaviour. It should be
delete[] str;
ostream& operator<<( ostream& os, String& str ) {
* *char *s = str.str;
* *while ( *s ) os.put( *s++ );
* *return os;
}
You don't change the str argument, so pass a const reference.
And code
* * os << str.str;
* * return os;
seems better to me
String&
String::operator+=( 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::operator+( 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;
}
--
Tadeusz B. Kopec (tko...@NOSPAMPLEASElife.pl)
A day for firm decisions!!!!! *Or is it?- Masquer le texte des messages précédents -
- Afficher le texte des messages précédents -- Masquer le texte des messages précédents -
- Afficher le texte des messages précédents -
Thank you very much. I am really new to CPP, quite different from C.
Seems too much implicit in CPP, interesting. Happy holidays. This discussion thread is closed Replies have been disabled for this discussion. Similar topics
3 posts
views
Thread by Philippe Guglielmetti |
last post: by
|
3 posts
views
Thread by Roman Simkin |
last post: by
|
20 posts
views
Thread by Sam |
last post: by
|
12 posts
views
Thread by Steven T. Hatton |
last post: by
|
9 posts
views
Thread by jlopes |
last post: by
|
8 posts
views
Thread by Crash |
last post: by
|
39 posts
views
Thread by utab |
last post: by
|
5 posts
views
Thread by sunny |
last post: by
|
4 posts
views
Thread by grizggg |
last post: by
|
16 posts
views
Thread by John Doe |
last post: by
| | | | | | | | | | |