Operator overloading C++

Questions about programming languages and debugging
Post Reply
User avatar
l0ngb1t
Fame ! Where are the chicks?!
Fame ! Where are the chicks?!
Posts: 598
Joined: 15 Apr 2009, 16:00
15
Contact:

Operator overloading C++

Post by l0ngb1t »

ok am trying to understand what operator overloading does and why we use it
but am having trouble getting it :S:S:S :oops:
can any one explain it to me in an easy way (suck-o style :P)
There is an UNEQUAL amount of good and bad in most things, the trick is to work out the ratio and act accordingly. "The Jester"

User avatar
IceDane
Fame ! Where are the chicks?!
Fame ! Where are the chicks?!
Posts: 197
Joined: 12 Aug 2009, 16:00
14

Post by IceDane »

Operator overloading is sometimes useful and sometimes necessary.

Let's say you're going to write a string class of your own.

Code: Select all

class MyString
{
public:
    MyString(char* str)
    {
          theString_ = new char[strlen(str) + 1];
          strcpy(theString_, str);
     }
private:
    char* theString_;
};
Some people would definitely want to do something like:

Code: Select all

MyString lol("cake");
MyString wat = lol;
Now, C++ will create an assignment operator for you if you don't provide one yourself, by simply doing member-to-member assignment. Seeing as your string is contained in an array(pointed to by a pointer), this means that if you do the above without overloading the assignment operator, this will happen:

Code: Select all

MyString lol("cake");
// lol.theString_ contains some address like 0x100
// Now wat = lol, and the default assignment operator
// simply does:
// wat.theString_ = lol.theString_
MyString wat = lol;
// Assuming we can do lol[index] on our string, and we want to modify
// one of the strings, like this:
lol[0] = 'W';
// We are directly modifying the string at address 0x100, which means that
// Both strings get modified
So, because the default assignment operator simply copies the address, we're modifying both strings at the same time - they both contain a pointer to the same string.
We can solve this by writing our own assignment operator, that copies the string into new memory.
This goes inside our class, in public:

Code: Select all

MyString& operator=(const MyString& rhs)
{
     // Note that we have access to all of rhs's members, regardless of 
     // Their access modifiers.
     theString_ = new char [strlen(rhs.theString_) + 1];
     strcpy(theString_, rhs.theString_);
}
And now we can modify the individual strings without modifying both of them.

There are many other uses for operator overloading -- as an example, or an exercise, you could try implementing a simple string class of your own, where you also overload the + and += operators to concatenate strings together.

If this wasn't what you were looking for, please be a bit more specific about what you want to know.

User avatar
l0ngb1t
Fame ! Where are the chicks?!
Fame ! Where are the chicks?!
Posts: 598
Joined: 15 Apr 2009, 16:00
15
Contact:

Post by l0ngb1t »

ok thank you ...:D
way much clearer :D
i appreciate it mate ...
There is an UNEQUAL amount of good and bad in most things, the trick is to work out the ratio and act accordingly. "The Jester"

Post Reply