473,508 Members | 2,342 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

What's wrong with the member function?

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::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;
}

Dec 28 '07 #1
7 1350
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

Dec 28 '07 #2
fl
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.
Dec 28 '07 #3
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.

Dec 28 '07 #4
fl
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 -
Dec 28 '07 #5
fl
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.
Dec 28 '07 #6
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?
Dec 28 '07 #7
fl
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.
Dec 28 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2505
by: Philippe Guglielmetti | last post by:
Look at these few lines of code: class A { public: virtual void f() { cout << "A";}}; class B : public A{public: static void f() { cout << "B"; }}; class C : public B{public: void f() { cout <<...
3
2364
by: Roman Simkin | last post by:
Hi, As far as I know, a static variable is a variable that belongs to a function or a class (are there any other options?). I've seen somewhere a function that *returns* static types - something...
20
2532
by: Sam | last post by:
Hi I'm learning to code with C++ and wrote some very simple code. I think it's consistent with every rule but always got compiling errors that I don't understand. The code include 5 files as...
12
3273
by: Steven T. Hatton | last post by:
This is something I've been looking at because it is central to a currently broken part of the KDevelop new application wizard. I'm not complaining about it being broken, It's a CVS images. ...
9
1894
by: jlopes | last post by:
There seems to bet no diff between a vitual method and an inheirited method. class A_Base { public: virtual void filter(){ /* some code */ } }; class D_of_A_Base : public A_Base {
8
1916
by: Crash | last post by:
2 Questions, but first Consider this: I'm looking at a C++ class (patterned using the Singleton Design Pattern). It has some public and private operations. It runs quite happily seving the time...
39
3140
by: utab | last post by:
Dear all, Is there a clear distinction how to decide which functions to be members of a class and which not How is your attitude (Your general way from your experiences ...) "If the...
5
1462
by: sunny | last post by:
hai i am not able to overload a member function of base class in derived calss.what is the wrong thning i am doing here in the following program. # include<iostream> using namespace std; ...
4
6672
by: grizggg | last post by:
I have searched and not found an answer to this question. I ran upon the following statement in a *.cpp file in a member function: static const char * const pacz_HTMLContentTypeHeader =...
16
3409
by: John Doe | last post by:
Hi, I wrote a small class to enumerate available networks on a smartphone : class CNetwork { public: CNetwork() {}; CNetwork(CString& netName, GUID netguid): _netname(netName),...
0
7229
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7129
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
7061
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5637
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3208
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3194
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1566
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
769
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
428
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.