overloading operator>> ()

Jul 15, 2015 at 9:11pm
In this book I'm learning C++ there's an example of a String class with an overloaded operator>>() via friend function.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>
#include <string.h>
using namespace std;


class String
{
public:
	// constr.
	String();
	String(const char* const); // converts C-string to String obj.
	String(const String&);
	~String();

	// overl. op's
	// (...)
	friend ostream& operator<<(ostream&, String&); 
	friend istream& operator>>(istream&, String&); 


	// General acc.
	int GetLen()const { return itsLen; }
	const char * GetString() const { return itsString; }
private:
	String(int); // private constr.
	unsigned short itsLen;
	char* itsString;
};

// overl. op<<() implementation, works !
ostream& operator<<(ostream& theStream, String& theString)
{
	theStream << theString.GetString();
	return theStream;
}

// overl. op>>() implementation, gives error !!!
istream& operator>>(istream& theStream, String& theString)
{
       theStream >> theString.GetString();
       return theStream;
}

int main()
{
// (...)
}



Unless I've made a typo, it's like this in the book.
What's wrong with the operator>>() implementation ?

Last edited on Jul 15, 2015 at 9:26pm
Jul 15, 2015 at 9:24pm
String::GetString returns a const char*
How can >> modify a constant on line 40?
Jul 15, 2015 at 10:12pm
Thanks.
Hm...the original program (where all the functions are implemented) compiles fine when I replace above

1
2
3
4
5
istream& operator>>(istream& theStream, String& theString)
{
       theStream >> theString.GetString();
       return theStream;
}


with

1
2
3
4
5
istream& operator>>(istream& theStream, String& theString)
{
	theStream >> theString.itsString;
	return theStream;
}


Why ? Isn't that basically the same thing, or in case I'm misunderstanding something, what's the difference ?

Jul 15, 2015 at 10:31pm
Isn't that basically the same thing

Erm, nope.

As kevinkjt2000 said, your GetString() returns a const char* i.e. one you can't change.

The data member is non-const, so you can change it.

But that code looks rather unsafe. You don't show the code for your constructors, but is there always going to be enough storage for the string being read by operator>>?

Andy
Jul 15, 2015 at 11:35pm

Erm, nope.

As kevinkjt2000 said, your GetString() returns a const char* i.e. one you can't change.

The data member is non-const, so you can change it.


Clear now as you say it. My head got a bit thick from too much code today....


But that code looks rather unsafe. You don't show the code for your constructors, but is there always going to be enough storage for the string being read by operator>>?


That code was just a first introduction to streams and overloading in general. I guess details such as you mention will follow further on in the chapter.

Marked as solved, thank you.

Last edited on Jul 15, 2015 at 11:36pm
Topic archived. No new replies allowed.