hello everybody,
i have a problem in implementing opearator overloading..
specially (iostream and [] operators)...please send me link 4 this..
hello everybody,
i have a problem in implementing opearator overloading..
specially (iostream and [] operators)...please send me link 4 this..
uhh- it depends on what you want to do with the iostream...
here's an example for the std::string class
and for the []Code:ostream& operator << (ostream& out, string &str) { out<<string.c_str(); return out; }
Code:int myArray::operator [](unsigned int index) { if(index >=0 && index < length) return pArray[index]; else { cerr<<"Error indexing"<<endl; return -1; } }
Asking the right question is sometimes more important than knowing the answer.
Please read the FAQ
C Reference Card (A MUST!)
Pointers and Memory
The Essentials
CString lib
here's an example for the std::string classIn this case itīs unnecessary to overload the std::string type because itīs already overloaded(default).Code:ostream& operator << (ostream& out, string &str) { out << str.c_str();//typo error I guess return out; }
01000111011011110110111101100100 011101000110100001101001011011100110011101110011 01100100011011110110111001110100 01100011011011110110110101100101 01100101011000010111100101110011 0110100101101110 01101100011010010110011001100101
Good things donīt come easy in life!!!
That's not so good because you can't do things like:Originally posted by Lynux-Penguin
Code:int myArray::operator [](unsigned int index) { if(index >=0 && index < length) return pArray[index]; else { cerr<<"Error indexing"<<endl; return -1; } }
myArray m(10);
m[5] = 3; // m[5] is not an lvalue
Plus, index is unsigned -- it's ALWAYS >= 0.
Instead:
Code:int & myArray::operator [](unsigned int index) { if(index < length) return pArray[index]; else throw std::range_error("Bad index"); }
oh yeah oops, well my excuse was it was 1AM and I was in the middle of an argument while posting.
and std::string is not overloaded, if you don't believe me try it.
with the following code:
you will receive the following error:Code:#include <iostream> using namespace std; int main() { string hi = "hello, world"; cout<<hi<<endl; return 0; }
-LCCode:D:\Program Files\Microsoft Visual Studio\MyProjects\haha\haha.cpp(7) : error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >'
Last edited by Lynux-Penguin; 08-30-2003 at 01:24 PM.
Asking the right question is sometimes more important than knowing the answer.
Please read the FAQ
C Reference Card (A MUST!)
Pointers and Memory
The Essentials
CString lib
And if you donīt trust me try
Code:#include <iostream> #include <string> using namespace std; int main() { string hi = "hello, world"; cout<<hi<<endl; return 0; }
01000111011011110110111101100100 011101000110100001101001011011100110011101110011 01100100011011110110111001110100 01100011011011110110110101100101 01100101011000010111100101110011 0110100101101110 01101100011010010110011001100101
Good things donīt come easy in life!!!
ouch.
I didn't know that. I like C more than C++ anyway. Besides I rarely ever use std classes, I wrote all the classes I ever use, CString, CArg, CArray, etc. CString is in my comment line if you can access the download... which it seems no one can because my host is having some 'issues' now -_-
but thanks for the input, I didn't know that. But my example and code still stands! without string.h my code is still usefull. ^_^
-LC
Asking the right question is sometimes more important than knowing the answer.
Please read the FAQ
C Reference Card (A MUST!)
Pointers and Memory
The Essentials
CString lib