473,503 Members | 3,171 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Explain Operator Overloaders & This-> to me me.

66 New Member
Could someone please help me understand some code. My teacher went over it last week, but I am lost on it still. Here is the code:

BigNum BigNum::operator+(const BigNum &b) {
return this->plus(b);
}

I would like two things explained to me about this code. First, what is an operator overloader used for? Then, what is 'this->' for in the code above.

Thanks - CTYD
Feb 19 '08 #1
3 1457
weaknessforcats
9,208 Recognized Expert Moderator Expert
An operator overload is to make your class easier to use. Say you have a Date class and you need to add a number of days to a Date. With an overload, you could have ciode like:
Expand|Select|Wrap|Line Numbers
  1. Date dt("02/192008");
  2.  
  3. Date duedate = dt +30;  //due in 30 days
  4.  
Without an overload you would need a function that has Date* and an int arguments to do the adding:
Expand|Select|Wrap|Line Numbers
  1. void AddToDate(Date* dt, Date* result, int incr)
  2. {
  3.      //code to add incr to dt and out answer in result
  4. }
  5.  
Then you code in main() would look like:
Expand|Select|Wrap|Line Numbers
  1. Date dt("02/192008");
  2.  
  3. Date duedate ;
  4. AddToDate(&dt, &duedate, 30);
  5.  
Which code looks easier an more intuitive to read??

There's your answer.

When you call a member function, the function needs to know which object it is supposed to use. The compiler passes the address of the correct object to use as an invisible first argument of a member function. That is the this pointer.

Notice on the member functions that there is no explicit argument for the object to use.

By coding this->, you identify a member variable or function. That is
this->Name identifies Name as a member variable. Some programmers use m_Name for the member variable but this does not identify the variable as a member variable. It's just a variable named m_Name. Maybe it's a member variable, maybe it's not. You have to use this->Name to be crystal clear.
Feb 19 '08 #2
CodeTilYaDrop
66 New Member
The operator overloader example does not register yet with me. I do understand my teacher's example a little more after looking at it though! This is good!

Then, I really appreciate the information about the 'this->'. This makes it a little more clear to me. Thank U- CTYD
Feb 19 '08 #3
weaknessforcats
9,208 Recognized Expert Moderator Expert
The operator overloader example does not register yet with me.
How about this:
Expand|Select|Wrap|Line Numbers
  1. char c = 'A';
  2. cout << c << endl;   //you see A
  3.  
  4. int i = c;
  5. cout << i << endl;     //you see 65
  6.  
How does the << operator do two different things ???

Answer: There are two << operator functins. One for char and one for int.
Expand|Select|Wrap|Line Numbers
  1. ostream& operator<<(ostream& os, int arg)
  2. {
  3.          //shows 65
  4. }
  5. ostream& operator<<(ostream& os, char arg)
  6. {
  7.          //Looks up thb 65 in a chart of symbols
  8.          //and shows the symbol A
  9. }
  10.  
Now when you define your own classes you can overload the << operator so objects of you class can use cout:
Expand|Select|Wrap|Line Numbers
  1. char c = 'A';
  2. cout << c << endl;   //you see A
  3.  
  4. int i = c;
  5. cout << i << endl;     //you see 65
  6.  
  7. Date dt(2,20,2008);
  8. cout << dt << endl;    //you see 02/20/2008
  9.  
Of course, you would have to write an operator<< function that can work with a Date:
Expand|Select|Wrap|Line Numbers
  1. ostream& operator<<(ostream& os, Date arg)
  2. {
  3.         //takes the arg apart into month day and year
  4.         //and displays the mon, day and year separated by slashes
  5. }
  6.  
Feb 20 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

3
8243
by: Victor Irzak | last post by:
Hello, I have an ABC. it supports: ostream & operator << I also have a derived class that supports this operator. How can I call operator << of the base class for derived object??? Is it...
1
2533
by: Tim Partridge | last post by:
I want operator<< to be a friend function of a class inside a namespace, but I don't know how. For example: #include <iostream> namespace ns { class foo { public: // there is something...
14
2508
by: bo | last post by:
And why and where one should use one vs. the other? Verbally, it seems like semantics to me--but obviously there is some actual difference that makes references different and or preferable over...
20
8118
by: Vivek N | last post by:
Hi Folks, This question may well have been asked and answered before. But, sorry that I couldn't find one from the archives. I typed up this program and compiled it with gcc 3.3.2 main() { int...
21
3000
by: siliconwafer | last post by:
Hi, In case of following expression: c = a && --b; if a is 0,b is not evaluated and c directly becomes 0. Does this mean that && operator is given a higher precedence over '--'operator? as...
4
6735
by: Rock | last post by:
I'm in the process of writing this program for complex numbers and I use DevC++. My professor on the other hand compiles on Borland 5.5. So I ocasionally save and run my work on Borland to see if...
6
9998
by: Geoffrey S. Knauth | last post by:
It's been a while since I programmed in C++, and the language sure has changed. Usually I can figure out why something no longer compiles, but this time I'm stumped. A friend has a problem he...
3
1330
by: Nathan Sokalski | last post by:
I have a VB.NET function that I am using in an ASP.NET page. The code creates a String, which contains && (the JavaScript Logical AND operator) and is used as part of the JavaScript sent to the...
1
2095
by: developereo | last post by:
Hi folks, Can somebodyshed some light on this problem? class Interface { protected: Interface() { ...} virtual ~Interface() { ... } public:
3
4223
by: Martin T. | last post by:
Hello. I tried to overload the operator<< for implicit printing of wchar_t string on a char stream. Normally using it on a ostream will succeed as std::operator<<<std::char_traits<char> will...
0
7063
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
6970
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7441
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4987
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4663
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3146
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1489
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
720
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
366
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.