Thread: Purpose of Operator Overloading

  1. #1
    Registered User
    Join Date
    Mar 2006
    Location
    Ohio
    Posts
    37

    Purpose of Operator Overloading

    I was reading an article about this and I'm totally lost as to what the purpose of using operator overlaoding is. Could someone give me an explaination as to what it's main purpose is?

    While I'm at it, I'm also confused on the purpose of copy constructors. I have an idea as to what they're used for so I'm gonna shoot and if it's wrong or anything, correct me because I'm sure I'm thinking the totally wrong thing.

    I think a copy constructor would be more useful when you have a VERY VERY generic class that you want to create more specific classes out of but use the private members of the generic class in the more specific one.

    Again, I'm probably thinking the totally wrong thing but I'm lost so any help would be appreciated. Thanks in advance.

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    I was reading an article about this and I'm totally lost as to what the purpose of using operator overlaoding is. Could someone give me an explaination as to what it's main purpose is?
    Take the string class for instance. They overloaded the + operator so that you could concatenate strings with it.
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Mar 2006
    Location
    Ohio
    Posts
    37
    Take the string class for instance. They overloaded the + operator so that you could concatenate strings with it.
    So in other words, it's more or less a way to modify the behavior of an operator to better suit your needs?

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Right. Another example is cout and cin. The << and >> operators are normally used for bitshifting, but they've been overloaded.
    If you understand what you're doing, you're not learning anything.

  5. #5
    Registered User OnionKnight's Avatar
    Join Date
    Jan 2005
    Posts
    555
    The purpose of overloading operators is to make code pretty.

    C:
    strcat(strcpy(string3, string1), string 2);

    C++:
    string3 = string1 + string2;

  6. #6
    Registered User
    Join Date
    Mar 2006
    Location
    Ohio
    Posts
    37
    Ah I see. That clears that issue up. Thanks for the help.

  7. #7
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    As for the copy constructor, let's say you have a Widget:

    Widget a(1, 2);

    and you wanted to make b exactly like Widget a. You can use the copy constructor to make more Widgets out of other Widgets.

    Widget b(a);

    That's what the copy constructor does.

  8. #8
    Registered User
    Join Date
    Mar 2006
    Location
    Ohio
    Posts
    37
    As for the copy constructor, let's say you have a Widget:

    Widget a(1, 2);

    and you wanted to make b exactly like Widget a. You can use the copy constructor to make more Widgets out of other Widgets.

    Widget b(a);

    That's what the copy constructor does.
    Okay so by using the copy constructor, it basically saves you from having to write code for classes that are identical?

    Taking from your example, I'm guessing that widget b would have the same properties as widget a?

  9. #9
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    No, the purpose of writing a copy constructor is to take advanced action on copying that the version created by the compiler doesn't do automatically. citizen's post was misleading.

    The typical example is a class that allocates memory inside and stores a pointer to it, then frees that memory in the destructor. (A string class would do that.) If the author doesn't write a copy constructor, the compiler will generate one that will just copy the pointer. Suddenly, both string objects (the old and the new one) point to the same memory, resulting in that memory being freed twice, which is an access violation. A custom copy constructor would allocate new memory for the copy and transfer the string contents over.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  10. #10
    Registered User
    Join Date
    Mar 2006
    Location
    Ohio
    Posts
    37
    hmmm That was slightly confusing.

    Okay lets say I have a class called enemy and it looks something like this:

    Code:
    class enemy{
    private:
         int a, b;
    public:
         enemy();
         enemy(int a, int b);
         //copy constructor
         enemy(enemy &newEnemy);
    };
    Now in my main function, I have the following statements:

    Code:
    enemy enemy1(5, 7);
    //using the copy constructor
    enemy enemy2(enemy1);
    The first statement instanciates the enemy class in enemy1. The second statement instanciates the enemy class again in enemy2. Enemy1 and enemy 2 both have private members a and b. Now assuming you code the copy constructor to copy the values of private members a and b, if I were output the values of enemy1.a and enemy2.a, they would be the same right?
    Last edited by xmltorrent; 08-09-2006 at 03:48 PM.

  11. #11
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Yes, they would be the same. But in this case you wouldn't need a copy constructor. The compiler provides one for you. You could write enemy b(a) even if you didn't write the copy constructor.

    You'll need it for more complicated classes. Classes that allocate memory dynamically (because two separate objects shouldn't use the same piece of free memory).
    Or for classes that have static members. For example, if you used a static variable to keep track of how many objects exist (in constructor increment it, in destructor decrement). Now the compiler-generated copy constructor wouldn't know that it needs to increment the static counter, and this would mess up the counting.

    Here's a tutorial: http://cplus.about.com/od/beginnerct.../aa072802a.htm

  12. #12
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    Overloaded operators don't always have to be a member of a class - if you wish to customise behaviour for operators involving classes which you have no control over, (such as standard library classes) then you can overload the operator outside the class, eg,
    Code:
    #include <iostream>
    #include <ostream>
    using namespace std;
    
    class MyDate
    {
        int day, month, year;
    public:
        MyDate(int d, int m, int y) : day(d), month(m), year(y) {}
        friend ostream& operator<<(ostream& os, MyDate md);
    };
    
    ostream& operator<<(ostream& os, MyDate md)
    {
        os << md.day << '/' << md.month << '/' << md.year << '\n';
        return os;
    }
    
    int main()
    {
        MyDate foo(15, 8, 1982);
        cout << foo;
    }
    This snippet outputs:
    Code:
     15/8/1982
    I find this method extremely useful for manipulating text i/o
    Last edited by Bench82; 08-09-2006 at 06:29 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. State Manager
    By Dark_Phoenix in forum Game Programming
    Replies: 1
    Last Post: 03-25-2007, 09:18 AM
  2. My purpose, what do I do?
    By jaylc185 in forum Game Programming
    Replies: 5
    Last Post: 05-25-2005, 10:15 AM
  3. Retro gaming purpose PC
    By Shadow in forum Tech Board
    Replies: 1
    Last Post: 11-08-2003, 06:09 PM
  4. 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
  5. What is the purpose of the struct keyword?
    By Gamma in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2002, 03:56 AM