Forums
New posts
Search forums
Members
Current visitors
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
Forums
Archive
Archive
C++
Operator overloading
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
Reply to thread
Message
[QUOTE="David White, post: 1453499"] No need to be so defensive. Your question is a reasonable one. operator+ requires two arguments (because it adds two values together), so this looks like a declaration from the definition of class comp. class comp { public: // members comp operator+(comp b); // more members }; The object you call the function for is the first argument, and b is the second argument. This is where you call the operator declared above: comp a(1); // assume constructor with numerical arg comp b(2); comp c = a.operator+(b); // 1 + 2 This computes a + b (1 + 2) and stores the result in c. It's an operator because you normally use it like this: c = a + b; not: c = a.operator+(b); Your book should discuss the a + b form somewhere, because being able to write a + b is the reason you would choose to overload operator+. If you are only going to call it as a function you might as well just have a function called 'add' and call it like this: a.add(b); They get chewed out for asking off-topic questions. Your question is on-topic. Yes, you are. BTW, these kinds of operator overloads are normally implemented as global functions instead of as class member functions, because there are advantages in using global functions. I hope your book covers that. DW [/QUOTE]
Verification
Post reply
Forums
Archive
Archive
C++
Operator overloading
Top