Already overloaded operators <<,>>,= etc are used many times.
An example that i was thinking of is when we add strings say :
string name = string("munish") + "kumar";
the + operator is overloaded in the string class.
but when we add numbers like 1 + 2( doesn't seem like an overloaded operator call)
I was just wondering how does it happens does the compiler does a binary additon.
I don't need to worry about it much though if the compiler does it so, just a matter of curiosity for me.
Primitive types don't implement operator+
which is actually a function with a weird name. Addition for Primitive type is carried out by CPU instruction such as :
addl %edx,(%eax) //this adds two integral values loaded at edx and eax
You implement operator+
for user-defined types, and compiler generates lots of CPU instructions to carry out the task which you write in operator+
.
I think you're confusing two things: operator overloading, and how you
define an operator for a user defined type. Operator overloading isn't
new; it exists already in C, where +
is overloaded on int
, unsigned
int
, long
, unsigned long
, float
, double
and long double
. In
C++, you can add operators for user defined types, by defining a
function named operator+
, so the list of overloads is longer. And to
make things more coherent, the way the compiler resolves the overload is
to add synthesized signatures along the lines of operator+(int, int)
to the list of overloads. If the overload resolution then ends up
choosing one of these synthesized signatures, it uses the built-in
operator, generating whatever machine code is necessary (which may
involve a function call at the machine code level). The fact that it
chooses one of the "build-in" overloads does have repercussions in the
rest of the code: a user defined operator is considered a function call,
with the corresponding sequence points and all of the rest, and the
compiler is not allowed to make any assumptions concerning its
semantics—in particular, the compiler cannot assume that a user
defined operator+
is associative or commutative. A build-in operator
(with a very few exceptions) does not introduce any sequence points, and
the compiler knows its semantics, and can optimize in consequence.
Note that the generated machine code may not be that different. I've
used machines where the compiler had to call a function to multiply
int
s, and the compiler can (and often will) inline a user defined
operator. What changes is the rules I mentionned above: the presence of
sequence points in user defined operators, and the fact that the
compiler cannot make assumptions concerning the various mathematical
laws which might apply.
You can overload the operators on custom data types i.e classes.. int is a primitive data type and the operators on primitive types are implemented by the compiler itself, basically it is just a simple CPU instruction to add int etc
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With