Overloading the << and >> operators

This is a discussion on Overloading the << and >> operators within the C++ Programming forums, part of the General Programming Boards category; Okay, I know how to overload the operators when using ostream ( << ) and istream ( >> ), though ...

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    110

    Overloading the << and >> operators

    Okay, I know how to overload the operators when using ostream ( << ) and istream ( >> ), though what I want to be able to do is something like this.

    Code:
      MyDataType someObject;
      string someSentance;
    
      someObject << someSentance;
    any sugestions would be greately apreciated.

    Thanks,

    WebmasterMattD
    WebmasterMattD
    WebmasterMattD.NET

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    *sigh*

    Code:
    class myClass {
    protected:
        string internal_string;
    
    public:
        myClass();
        string& operator << (string s)  {
            this->internal_string = s;
            return this->internal_string;
        }
    };
    
    myClass :: myClass() {
         this->internal_string = "";
    }
    You could have looked this up online you know.

    [edit]
    tired....making typoes in code.
    [/edit]
    Last edited by master5001; 10-15-2002 at 01:32 AM.

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    110
    Thanks for your help.

    I had a lapse of memory amongst a couple of hours of laziness.

    Later,
    WebmasterMattD
    WebmasterMattD.NET

  4. #4
    Registered User
    Join Date
    Oct 2002
    Posts
    160
    Why did you use the this pointer? I know what that pointer does and all that but it wasn't, en my eys anyway, necesary to use hear.
    Well english isn't my first language, (it's instead a useless language called danish which only 5 milion people speak!!) so if you think my grammar SUCKS (it does by the way) than you're more then welcome to correct me.
    Hell I might even learn something

  5. #5
    btq
    btq is offline
    julie lexx... btq's Avatar
    Join Date
    Jun 2002
    Posts
    161
    Why did you use the this pointer? I know what that pointer does and all that but it wasn't, en my eys anyway, necesary to use hear.
    in my eyes it seemed kinda necessary
    it's used to set the string internal_string of that instance, and
    that should be the simplest way to do it..
    what else do you suggest?

    /btq
    ...viewlexx - julie lexx

  6. #6
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    How about removing this? Since there is no naming conflict, you don't need it .
    Code:
    string& operator << (string s)  
    {
       internal_string = s;
       return internal_string;
    }
    <edit>
    Of course, there's nothing wrong with using it, but ignoring it saves some bytes in the file
    </edit>
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  7. #7
    btq
    btq is offline
    julie lexx... btq's Avatar
    Join Date
    Jun 2002
    Posts
    161
    err...at least it looks more beautiful with the 'this'

    /btq
    ...viewlexx - julie lexx

  8. #8
    Registered User
    Join Date
    Apr 2002
    Posts
    110
    I agree, by using the this pointer, you can do
    someclass << somestring << someotherstring;

    instead of
    someclass << somestring;
    someclass << someotherstring;

    later,
    WebmasterMattD
    WebmasterMattD.NET

  9. #9
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Originally posted by WebmasterMattD
    I agree, by using the this pointer, you can do
    someclass << somestring << someotherstring;

    instead of
    someclass << somestring;
    someclass << someotherstring;

    later,
    No, no, no!!

    "this->whatever" and "whatever" are equivalent!

    The only, and I mean only, reason to use this in this way is for nicer looking code (and I don't think it looks nicer at all).
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  10. #10
    Seeking motivation... endo's Avatar
    Join Date
    May 2002
    Posts
    537
    I dont use this in member functions unless its necessary. It makes the code harder to read, and takes me more time to type. Unneccessary most of the time
    Couldn't think of anything interesting, cool or funny - sorry.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Overloading << and >>
    By MMu in forum C++ Programming
    Replies: 1
    Last Post: 04-21-2008, 06:49 AM
  2. Overloading fstream's << and >> operators
    By VirtualAce in forum C++ Programming
    Replies: 2
    Last Post: 04-09-2007, 03:17 AM
  3. operators << and >>
    By crvenkapa in forum C++ Programming
    Replies: 1
    Last Post: 04-06-2007, 03:10 PM
  4. Compile errors with overloading >> operators
    By GMHummerH1 in forum C++ Programming
    Replies: 1
    Last Post: 12-19-2004, 06:13 PM
  5. << and >> overloading
    By Armatura in forum C++ Programming
    Replies: 2
    Last Post: 12-07-2003, 05:19 PM

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