Operator Overloading......... (help)

This is a discussion on Operator Overloading......... (help) within the C++ Programming forums, part of the General Programming Boards category; hello everybody, i have a problem in implementing opearator overloading.. specially (iostream and [] operators)...please send me link 4 this.....

  1. #1
    Registered User
    Join Date
    Aug 2003
    Posts
    1

    Operator Overloading......... (help)

    hello everybody,
    i have a problem in implementing opearator overloading..
    specially (iostream and [] operators)...please send me link 4 this..

  2. #2
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533
    uhh- it depends on what you want to do with the iostream...

    here's an example for the std::string class
    Code:
    ostream& operator << (ostream& out, string &str)
    {
    	out<<string.c_str();
    	return out;
    }
    and for the []
    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

  3. #3
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343
    here's an example for the std::string class
    Code:
    ostream& operator << (ostream& out, string &str)
    {
            out << str.c_str();//typo error I guess
    	return out;
    }
    In this case itīs unnecessary to overload the std::string type because itīs already overloaded(default).
    01000111011011110110111101100100 011101000110100001101001011011100110011101110011 01100100011011110110111001110100 01100011011011110110110101100101 01100101011000010111100101110011 0110100101101110 01101100011010010110011001100101
    Good things donīt come easy in life!!!

  4. #4
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    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;
    	}
    }
    That's not so good because you can't do things like:

    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");
    }

  5. #5
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533
    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:
    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	string hi = "hello, world";
    	cout<<hi<<endl;
    	return 0;
    }
    you will receive the following error:

    Code:
    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> >'
    -LC
    Last edited by Lynux-Penguin; 08-30-2003 at 02: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

  6. #6
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343
    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!!!

  7. #7
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533
    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

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21