Overload operator>> for my custom String class

Hello.

I'm making my own String class where I hold a char* mainString and a int size

I did a lot of methods and operator overloading. Now I'd like to know how read a line (until the '\n' character) and put that into my char* mainString

1
2
MyString my;
cin >> my;


Thanks!
Now I'd like to know how read a line (until the '\n' character) and put that into my char* mainString

You could overload std::istream::getline() for your class.

By the way normally the extraction operator>> reads until it finds a white space character, not necessarily the end of line character.

In fact, I want to read until the line ends (spaces included)
Then I recommend you overload the getline() function, not the extraction operator. IMO making your overloads different just to make them different will cause confusion for everyone but yourself. And you'll probably be confused after a while as well.
But I need those for my class
But I need those for my class

What do you need for your class?

An overload for the operator>> so that I can use the cin

1
2
MyStringClass my;
cin >> my;
Okay, what have you tried?

And I still recommend you make your overload act the same as the standard operator>>, ie stops when it encounters any whitespace.

closed account (D80DSL3A)
There is a problem to solve in allocating enough space for an unknown # of characters coming in the stream. I solved this by declaring a local char array char buff[buffSz]; for use as an input buffer, then repeatedly allocate larger blocks to mainString as buff is filled over and over from the stream. I found the std::istream::get( char*, streamsize n, char delim ) useful.
See: http://www.cplusplus.com/reference/istream/istream/get/
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
std::istream& operator>>( std::istream& is, myStr& s )
{
    const int buffSz = 6;// low for easy testing
    char buff[buffSz];    
    s.sz = 0;
    int n = 0;// # chars extracted each get

    while( is.get( buff, buffSz, '\n' ) )// call will fail if '\n' is next. ie. it won't return just '\0'
    {
        // find n
       // new array size = exist + n + 1
       // the usual allocate/ copy/ delete steps
    }

    return is;
}

Or, have you found a way to avoid having to read in chunks like this?

edit: Brain fart
Last edited on
I tried doing this way:

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
friend istream& operator >> (istream& IN, EString& mystring)
{		
	const int buffSz = 100;	
	char buff[buffSz];

	IN.get(buff, buffSz, '\n');

	mystring = EString(buff);

	return IN;
}


int main(int argc, char** argv)
{
	EString name{ "example" };

	cin >> name;

	cout << "You typed: " << name << endl;


	_getch();
	return 0;
}


This is what happens before crashing: http://prntscr.com/awap3q

Last edited on
closed account (D80DSL3A)
Then I believe the issue is with your operator=. Your code should work.
I've just overloaded the operator= and now it works fine.

My questions is: before, I wasn't overloading the operator= and the program crashed.

But why? Wasn't the compiler clever enough using the "default" assignment operator?
closed account (D80DSL3A)
Yes it was. The default operator is inappropriate in this case. We must write one ourselves.
I hope you've also written a copy constructor. The default version of that won't do either.
Yes I have.

Oh! Another question!

This is the operator>> overload

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
	friend istream& operator >> (istream& IN, EString& mystring)
	{		
                // allocating random space
		const int buffSz = 100;	
		char buff[buffSz];

		// getting the string from the stream
		IN.get(buff, buffSz, '\n');

		// copying buff into myString
		mystring = EString(buff);

		// clearing the stream buffer
		IN.clear();
		IN.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

		return IN;
	}


You see... I'm allocating 100 characters but what If I wanted not to allocate an exact number but let the ISTREAM determine the number of chars typed?
closed account (D80DSL3A)
Are you asking what if we knew how many chars are coming in the stream before we read so we can allocate the exact right amount of space right up front?

That would be great, but I don't know of any way to obtain that number. I think we're stuck with reading from the stream until it's empty. Then we know how many chars there were!
IN could be an ifstream (reading from a file) and there may be millions of chars coming!

You need to consider more than 100 chars may be in the stream, so buff may get filled repeatedly.
Have you overloaded operator+= yet? That would perform string concatenation I believe.
It could be handy in a while loop where buff gets filled more than once from the stream.
No you misunderstood.

I am allocating 100 chars by default. The problem is: what if the user types more than 100 chars before pressing ENTER (\n) ?

I want my stream not to have limits!
Last edited on
Topic archived. No new replies allowed.