User Name Password Register
DaniWeb IT Discussion Community
All
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
Reply
Join Date: Jun 2006
Location: USA
Posts: 1,082
Reputation: Duki has a spectacular aura about Duki has a spectacular aura about Duki has a spectacular aura about 
Rep Power: 7
Solved Threads: 4
Duki's Avatar
Duki Duki is offline Offline
Veteran Poster

Member Operators and Automatic type Conversions

  #1  
Sep 20th, 2007
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
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jun 2006
Location: USA
Posts: 1,082
Reputation: Duki has a spectacular aura about Duki has a spectacular aura about Duki has a spectacular aura about 
Rep Power: 7
Solved Threads: 4
Duki's Avatar
Duki Duki is offline Offline
Veteran Poster

Re: Member Operators and Automatic type Conversions

  #2  
Sep 20th, 2007
Originally Posted by Duki View Post
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
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,541
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 40
Solved Threads: 972
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: Member Operators and Automatic type Conversions

  #3  
Sep 21st, 2007
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.
<<Freelance Programmer>> << Hobby Site>>
Signature links for sale. PM me for details
Reply With Quote  
Join Date: Jun 2006
Location: USA
Posts: 1,082
Reputation: Duki has a spectacular aura about Duki has a spectacular aura about Duki has a spectacular aura about 
Rep Power: 7
Solved Threads: 4
Duki's Avatar
Duki Duki is offline Offline
Veteran Poster

Re: Member Operators and Automatic type Conversions

  #4  
Sep 21st, 2007
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.
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
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,541
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 40
Solved Threads: 972
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: Member Operators and Automatic type Conversions

  #5  
Sep 21st, 2007
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.
Last edited by Ancient Dragon : Sep 21st, 2007 at 12:39 am.
<<Freelance Programmer>> << Hobby Site>>
Signature links for sale. PM me for details
Reply With Quote  
Join Date: Jun 2006
Location: USA
Posts: 1,082
Reputation: Duki has a spectacular aura about Duki has a spectacular aura about Duki has a spectacular aura about 
Rep Power: 7
Solved Threads: 4
Duki's Avatar
Duki Duki is offline Offline
Veteran Poster

Re: Member Operators and Automatic type Conversions

  #6  
Sep 21st, 2007
alright i'll try that. thanks!
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
Reply With Quote  
Join Date: Aug 2005
Posts: 4,832
Reputation: iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light 
Rep Power: 17
Solved Threads: 324
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Industrious Poster

Re: Member Operators and Automatic type Conversions

  #7  
Sep 21st, 2007
> 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
... the hat of 'is this a cat in a hat?'
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C++ Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 3:24 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC