•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 456,494 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,692 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 568 | Replies: 6
![]() |
Could someone possibly post a brief explanation of the differences between member and nonmember operator overloading?
It is practically impossible to teach good programming style to students that have had prior exposure to Basic; as potential programmers they are mentally mutilated beyond hope of regeneration.
-Edsger Dijkstra
-Edsger Dijkstra
•
•
•
•
Could someone possibly post a brief explanation of the differences between member and nonmember operator overloading?
I was just thinking... if someone were to write a C++ tutorial on nothing but operator overloading and friends... that would be kick A; i would add them to my buddy list even.
Last edited by Duki : Sep 20th, 2007 at 10:46 pm.
It is practically impossible to teach good programming style to students that have had prior exposure to Basic; as potential programmers they are mentally mutilated beyond hope of regeneration.
-Edsger Dijkstra
-Edsger Dijkstra
•
•
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,541
Reputation:
Rep Power: 40
Solved Threads: 972
There's not that much of a difference -- one is a member of a c++ class and the other isn't.
#include <iostream>
using namespace std;
class MyClass
{
public:
void operator<<(std::string str) { cout << str;}
std::string SayHello() {return "Hello\n";}
};
// non-member operator
ostream& operator<<(ostream& stream, class MyClass& obj)
{
stream << obj.SayHello();
return stream;
}
int main()
{
MyClass obj;
// invoke member operator
obj << "Hello\n";
// invoke non-member operator
cout << obj;
return 0;
}
Last edited by Ancient Dragon : Sep 21st, 2007 at 12:14 am.
I honestly don't understand overloading operators at all. Something about the left hand side and right hand side being different and whatnot... it's all a big blur right now. I read about 30 pages of my c++ text and finally just gave up, deciding to go to bed and try again tomorrow.
Maybe if you or someone else gets time, you could briefly explain how they work? I know that's a big question to ask, so if you can't no worries, i'll figure it out eventually.
see ya tomorrow.
Maybe if you or someone else gets time, you could briefly explain how they work? I know that's a big question to ask, so if you can't no worries, i'll figure it out eventually.
see ya tomorrow.
Last edited by Duki : Sep 21st, 2007 at 12:16 am.
It is practically impossible to teach good programming style to students that have had prior exposure to Basic; as potential programmers they are mentally mutilated beyond hope of regeneration.
-Edsger Dijkstra
-Edsger Dijkstra
•
•
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,541
Reputation:
Rep Power: 40
Solved Threads: 972
Do you understand how function overloading works? If you do then think of operator overloading in the same way. They work because of the different parameters (if they have the same number of parameters then the parameter types must be different). The c++ compiler mangles function names by including the parameters are part of the function name, which is why you can have a c++ class with two or more functions with the same name but different parameters. Exactly how function names are mangled is compiler dependent -- compilers can do it however they wish.
I think the easiest way to get all that strait is to write a small test program, similar to what I posted above, compile for debug, then use the debugger to step through the code one line at a time so that you can see the execution flow.
I think the easiest way to get all that strait is to write a small test program, similar to what I posted above, compile for debug, then use the debugger to step through the code one line at a time so that you can see the execution flow.
Last edited by Ancient Dragon : Sep 21st, 2007 at 12:39 am.
> I was just thinking... if someone were to write a C++ tutorial on nothing but operator overloading and friends
What's wrong with google?
http://www.parashift.com/c++-faq-lit...erloading.html
http://www.cplusplus.com/doc/tutorial/classes2.html
What's wrong with google?
http://www.parashift.com/c++-faq-lit...erloading.html
http://www.cplusplus.com/doc/tutorial/classes2.html
... the hat of 'is this a cat in a hat?'
![]() |
•
•
•
•
•
•
•
•
DaniWeb C++ Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Similar Threads
- PHP Form Validation ??? (PHP)
- Type casting and type conversions (C)
- Problems with changing values returned by RegQueryValueEx (C)
- help on data type conversion (Visual Basic 4 / 5 / 6)
- automatic type conversion (PHP)
- A few questions about C++ (C++)
- User defined functions (C++)
Other Threads in the C++ Forum
- Previous Thread: Terminal size
- Next Thread: Numbering with index?



Linear Mode