c++ operator overload

This is a discussion on c++ operator overload within the C++ Programming forums, part of the General Programming Boards category; what i know until now is that we can overload << >> + - ++ .. but i cannot understand ...

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    65

    c++ operator overload

    what i know until now is that we can overload << >> + - ++ ..
    but i cannot understand when we declare the operator we put this & before the variable also sometimes we write 2 variables in operator declare
    other times when we overload << or >> at operator function we first put ostream and istream (if its >> or <<) first and we return *this which i cannot understand what is it
    anyway i need a good tutorial to explain my above queries and general about overloading

  2. #2
    Registered User
    Join Date
    Nov 2011
    Posts
    37
    Simple google search could give you so many results.
    here is one of them:
    TUTORIAL: Defining and Overloading Operators in C++

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    For the "&" question, when dealing with overloading operators we typically have custom objects that we are passing around and these objects are much more efficiently passed around when they are references (which is what the "&" signifies). Passing these objects the other way typically involves wasteful and time-consuming copying of the objects as they are passed into the overloaded operator function code.

    Overloading the stream insertion/extraction operators <</>> involves writing to streams. I/O streams in C++ are typically derived from either ostream (ofstream, cout/cerr, etc...) or istream (ifstream, cin, etc...) and so that is what you pass. When you read/write to such streams the stream is passed into the overloaded function along with the object itself to do the actual writing. When the writing is done we need to pass the modified stream back to the caller - this is required because it allows for the chaining of multiple <</>> calls. Without passing the modified stream back we would be unable to chain calls.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Registered User
    Join Date
    Sep 2008
    Posts
    200
    Quote Originally Posted by cable View Post
    i cannot understand when we declare the operator we put this & before the variable... and we return *this which i cannot understand
    You need to read up on references - have a look at your C++ book-of-choice or the one on this site.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. overload operator[] help please
    By ychen42 in forum C++ Programming
    Replies: 12
    Last Post: 11-10-2010, 08:03 PM
  2. Operator Overload
    By MarlonDean in forum C++ Programming
    Replies: 3
    Last Post: 06-17-2008, 03:36 PM
  3. operator overload
    By maxcat in forum C++ Programming
    Replies: 2
    Last Post: 03-17-2008, 06:07 AM
  4. Operator overload
    By Gordon in forum C++ Programming
    Replies: 3
    Last Post: 03-11-2008, 02:20 PM
  5. Overload operator
    By verbity in forum C++ Programming
    Replies: 13
    Last Post: 03-30-2007, 12:00 PM

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