Thread: Overloading operators

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    3

    Overloading operators

    I am learning more about the power of the c++ language from reading c++ beginner's guide, and I can't seem to understand why you would need want or desire to overload an operator. From what the tutorial says, overloading an operator is very powerful. What is so powerful about that. It seems like more of a hinderence than a help. But considering that I am new to c++ it probably won't make sense to me until I have an occasion for when I would use it. So my question to the c++ programing community at large is, when do you use overloading an operator? Any practical examples would be helpful. It seems to me that overloading an operator is nothing more than taking what the operator does and putting it into a function that industrializes it. Any help is appreciated.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by ugmusicbiz
    when do you use overloading an operator? Any practical examples would be helpful.
    It would be rather tedious to try and enumerate all reasonable uses of operator overloading (and there are many more possible abuses of operator overloading), but here are some examples:

    One use of operator overloading that you might have encountered, or will encounter very soon, is to overload operator= to perform copy assignment (and in fact it is known as the copy assignment operator). For example, for some class X, you might want to be able to write:
    Code:
    X a;
    X b;
    a = b;
    Later on when you have learnt about iterators and pointers, you will see that standard iterator syntax is inspired by pointer syntax (of which iterators are a generalisation). To make this syntax possible for iterators that are of class types, operator overloading is used.

    Yet another example is when people write arbitrary precision mathematics libraries: they might use operator overloading to make the syntax more "natural", e.g., just as you can write (a + b) for int variables a and b, they might want to allow their users to write (x + y) for some "bignum" variables x and y.

    Quote Originally Posted by ugmusicbiz
    It seems to me that overloading an operator is nothing more than taking what the operator does and putting it into a function that industrializes it.
    I am not sure what you mean, but you can think of it in this way: an overloaded operator is a function with a special name that allows it to be called differently from normal functions (but you can call it like you would any other function).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    This one takes me by surprise. Some people don't immediately understand what pointers are good for, but I didn't expect anyone not to understand the huge advantage of operator overloading.
    Some random examples that would be so ugly without operator overloading:
    Code:
    std::string a = "orange";
    std::string b = "apple";
    if (b < a)
        std::cout << "told you so!" << std::endl;
    Code:
    complex j(1, 2);
    complex k(-2, 1);
    if (j*k == k/j)
        std::cout << "didn't think so!" << std::endl;
    In fact if you make your own class to represent some kind of data and you don't provide the sensible set of operator overloads, then your class just sux. E.g. If you don't implement less-than then putting it in a std::set or sorting a vector of them etc just becomes unecessarily awkward.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. question about overloading operators
    By *DEAD* in forum C++ Programming
    Replies: 9
    Last Post: 05-08-2008, 10:27 AM
  2. Replies: 16
    Last Post: 10-27-2007, 12:42 PM
  3. Overloading fstream's << and >> operators
    By VirtualAce in forum C++ Programming
    Replies: 2
    Last Post: 04-09-2007, 03:17 AM
  4. operators overloading
    By waqasriazpk in forum C++ Programming
    Replies: 1
    Last Post: 07-26-2002, 01:05 AM
  5. Overloading operators...
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 11-21-2001, 08:24 PM