c++ overloading operator>> and operator<< with an arrayclass help please

wildwolf

Golden Member
Jan 9, 2000
1,690
0
76
Hello all.

I need help with the implementation of an arrayclass operator overloading.

I have created a class 'arrayclass' but need to overload the operator>> so that I can use:
cin >> myArray[0]; // get input from user and store into index 0 of custom arrayclass.

I also have to have it allow:
cin >> myArray; // allow user to input every index in the class (say...0 to 5)

I can get the "cin >> myArray" to work fine. Where I'm having trouble is implementing the one that allows a specific index of the array to be input. Any clues would be helpful....I'm assuming it'll still use the same instream& return value, but could very well be wrong.

....

Same thing with overloading<< (for cout).
I can implement the "entire array" being output...but not a specific index (although it appears to work w/o overloading it...I'm sure there are cases where it will NEED to be overloaded.

anybody?
 

Barnaby W. Füi

Elite Member
Aug 14, 2001
12,343
0
0
myArray[0] is just an expression. It returns whatever arrayclass::eek:perator[](int) returns. THAT is the type you need to be concerned with. As for the actual overloading, I assume you would need to define operator>>(istream, whatever-type-it-is-you're-dealing-with). If it's a built-in type, then it's already taken care of.
 

wildwolf

Golden Member
Jan 9, 2000
1,690
0
76
geez...do I feel dumb (for not just trying cin >> myArray[0]; )

I had already wrote the arrayclass::eek:perator[](int) overloaded function....but was assuming (my BIG mistake) that I would need to overwrite the istream & ostream for instances of cin/cout >>/<< myArray[index value] as well.

However, I do have an interesting situation now....

Is there any way to know in the arrayclass::eek:perator[] that the calling function was a cin vs. a cout?
ie if it's a cin, I would like to ignore the newline character from the input....and would need to know if it was a cin in order to do so, correct?


 

Barnaby W. Füi

Elite Member
Aug 14, 2001
12,343
0
0
Personally I would probably try to make it so that it either always strips the newline or it never does; I prefer consistency over built-in "intelligence" any day.

You could make two methods, with one ignoring the newline and one not, or a default option, like:

void arrayclass::addText(const std::string& text, bool stripnewline = false)

and then your operator>> could call addText with the second argument set to true. But personally I don't like doing stuff like that, I prefer to avoid options whenever possible, and just provide a clean and straightforward interface to do whatever might be needed. Just kinda depends on the situation.
 

wildwolf

Golden Member
Jan 9, 2000
1,690
0
76
Well, I have to strip it, but it has to be selective, I think.

For whatever reason, when I add it to my [] operator overload, it will cause every cout statement to pause between each line...I dunno, I was real tired last night, and will have more time to look more indepthly this afternoon, so perhaps the sleep was the best solution for me. :)

Btw, thanks for the insight and assistance.