Purpose of Overloading Operators

This is a discussion on Purpose of Overloading Operators within the C++ Programming forums, part of the General Programming Boards category; Whats the purpose of overloading operators? Why would I create a function for ++ if I could use ++ already? ...

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    51

    Purpose of Overloading Operators

    Whats the purpose of overloading operators? Why would I create a function for ++ if I could use ++ already?

    Thanks in advance.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Whats the purpose of overloading operators?
    Syntactic sugar, notational convenience, whatever neat little phrase you have for simplifying the programmer's job in reading and writing more intuitive code. Of course, often this convenience is had at the cost of safety.

    >Why would I create a function for ++ if I could use ++ already?
    You can't use ++ for a linked list, you must use

    p = p->next;

    However, you can overload the ++ operator for your list class so that p++ performs the same operation as p = p->next underneath the hood. Notational convenience.

    -Prelude
    My best code is written with the delete key.

  3. #3
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    Code:
    class idiotic_example
    {
         private: 
                  int a;
         public:
                  idiotic_example() : a(0) {}
                  idiotic_example(int x) : a(x) {}
    };
    Given with the above code we can create idiotic_example objects that hold an int we set. How without overloading ++ can we do this...
    Code:
    idiotic_example This_is_stupid;
    This_is_stupid ++;
    Now its fairly obvious what that should do. It should increment the This_is_stupid.a and return its old value. Easy enough to write.
    Code:
    class idiotic_example
    {
         private: 
                  int a;
         public:
                  idiotic_example() : a(0) {}
                  idiotic_example(int x) : a(x) {}
                  int operator++ (int x)
                   {
                          int temp = a;
                          ++a;
                          return temp;
                    }
    
    };
    Problem solved. Now the ++ can work. But only the postfix form. The prefix form has not been overloaded. ok this was a quick and silly example but once you start writing your own classes you will love operator overloading.Try writing a string class or a bounds checked array class. You will need heavy operator overloading for those and will help you understand its need better.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  4. #4
    Registered User
    Join Date
    Feb 2003
    Posts
    51
    Wow, actually understand it. Thanks! This forum rocks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Overloading operators
    By ugmusicbiz in forum C++ Programming
    Replies: 2
    Last Post: 02-13-2009, 12:41 PM
  2. Purpose of Operator Overloading
    By xmltorrent in forum C++ Programming
    Replies: 11
    Last Post: 08-09-2006, 06:23 PM
  3. Overloading Operators
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 08-19-2002, 02:23 PM
  4. operators overloading
    By waqasriazpk in forum C++ Programming
    Replies: 1
    Last Post: 07-26-2002, 01:05 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM

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