C binary operator overloading

0 votes

As per Deitel's 9ed C++ How to program, p. 439-440:

A binary operator can be overloaded as a non-static member function with one parameter or as a non-member function with two parameters (one of those parameters must be either a class object or a reference to a class object).

So, what other sort of object is there beside a class object or a reference to a class object? I cannot think of anything.

Jun 6, 2022 in C++ by Nicholas
• 7,760 points
570 views

1 answer to this question.

0 votes

The language defines several fundamental types, such as int and double. 

Objects are instances of fundamental kinds.

Let's pretend you have:

struct Foo { ... };

You can use non-member functions to overload the operator+ function.

Foo operator+(Foo , int);
Foo operator+(Foo& , int);
Foo operator+(int, Foo);
Foo operator+(int, Foo&);

etc. 

One of the parameters in each of these circumstances is a Foo or a Foo&. 

Foo const& is another possibility. 

You cannot, however, overload it

int operator+(int, int);

because neither argument type is a class or a pointer to a class

answered Jun 21, 2022 by Damon
• 4,960 points

Related Questions In C++

0 votes
0 answers

Constructor Overloading in C++

My C++ overloading does not work as I expect it to: #include "Node.h" #include <iostream> Node::Node() { cout ...READ MORE

Jun 2, 2022 in C++ by Nicholas
• 7,760 points
396 views
0 votes
1 answer

C++ - Overloading vs Overriding in Inheritance

In C++, a derived class's method only overrides the base class's method if their declarations match (I say "match," but I'm not sure what the formal term is).  That is, all arguments must be of the same type, with the same const qualification.  If there are any mismatches, the derived class's method hides all methods with the same name rather than overriding.  This is what the "ERROR" in your image is attempting to convey.  So, in that image, / overrides in a comment is incorrect and misleading. Yes, many C++ instructors are unaware of these somewhat esoteric details. Furthermore, if you want to override, your base class's method must be virtual; otherwise, polymorphism will not work. . We could also say that the derived-class method hides the base-class method if it wasn't virtual.  The part about hiding, on the other hand, has almost no meaning here; what this term really means is that you're not in charge. Furthermore, overloading is the presence of multiple methods with the same name but different signatures, as you may have noticed. To be useful, they must all be present in the derived class; otherwise, they will be hidden if the derived class only has one method, fa1, and the other fa1 are in the base. There is, however, a syntax sugar that "copies" all fa1 from base to derived, removing all the hidden semantics: class A { public: void fa1(); ...READ MORE

answered Jun 7, 2022 in C++ by Damon
• 4,960 points
823 views
0 votes
1 answer

C++ - Overloading vs Overriding in Inheritance

In C++, a derived class's method only overrides the base class's method if their declarations match (I say "match," but I'm not sure what the formal term is).  That is, all arguments must be of the same type, with the same const qualification.  If there are any mismatches, the derived class's method hides all methods with the same name rather than overriding.  This is what the "ERROR" in your image is attempting to convey.  So, in that image, / overrides in a comment is incorrect and misleading. Yes, many C++ instructors are unaware of these somewhat esoteric details. Furthermore, if you want to override, your base class's method must be virtual; otherwise, polymorphism will not work.  We could also say that the derived-class method hides the base-class method if it wasn't virtual.  The part about hiding, on the other hand, has almost no meaning here; what this term really means is that you're not in charge. Furthermore, overloading is the presence of multiple methods with the same name but different signatures, as you may have noticed. To be useful, they must all be present in the derived class; otherwise, they will be hidden if the derived class only has one method, fa1, and the other fa1 are in the base.  There is, however, a syntax sugar that "copies" all fa1 from the base to the derived. class A { public: void fa1(); ...READ MORE

answered Jun 7, 2022 in C++ by Damon
• 4,960 points
1,048 views
0 votes
1 answer

Constructor Overloading in C++

In your function Object() { [native code] }, Node(Game(),v); does not work as expected.  It simply creates a temporary without actually using it, and has no effect.  When control passes over the ;, it immediately destroys the temporary. Initializing the members in each function Object() { [native code] } is the correct way to go.  You could put their shared code in a private init() member function and call it from each function Object() { [native code] }, as shown below: class Foo { public: ...READ MORE

answered Jun 21, 2022 in C++ by Damon
• 4,960 points
394 views
0 votes
1 answer

Syntax of priority queue

We must first include the queue header file in order to establish a priority queue in C++. #include <queue> Once we import this file, we ...READ MORE

answered May 31, 2022 in C++ by Damon
• 4,960 points
610 views
0 votes
0 answers

How to code a modulo (%) operator in C/C++/Obj-C that handles negative numbers

As a mathematician, one of my pet peeves of C-derived languages is : (-1) % 8 // comes out as ...READ MORE

Jul 27, 2022 in C++ by Nicholas
• 7,760 points
417 views
0 votes
1 answer

does java support operator overloading?

Java doesn't supports operator overloading because it's just a ...READ MORE

answered Jun 18, 2018 in Java by Akrati
• 960 points
41,733 views
0 votes
1 answer

setuptools: build shared libary from C++ code, then build Cython wrapper linked to shared libary

There is a seemingly undocumented feature of setup that ...READ MORE

answered Sep 11, 2018 in Python by Priyaj
• 58,020 points
686 views
0 votes
1 answer

What is the difference between operator overloading and operator overriding in C++?

Some people use the latter word to ...READ MORE

answered Aug 2, 2022 in C++ by Damon
• 4,960 points
1,929 views
0 votes
1 answer

Why does C++ need the scope resolution operator?

No. There is no scope resolution operator ...READ MORE

answered Jun 1, 2022 in C++ by Damon
• 4,960 points
868 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP