0% found this document useful (0 votes)
3 views3 pages

Chapter 7

The document discusses operator overloading in C++, outlining which operators cannot be overloaded and the rules governing operator functions. It explains how to define operator functions as either member or friend functions, detailing the invocation process for unary and binary operators. Additionally, it covers type conversions between incompatible types and the requirements for defining an overloaded casting operator function.

Uploaded by

Pandu Ranga
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views3 pages

Chapter 7

The document discusses operator overloading in C++, outlining which operators cannot be overloaded and the rules governing operator functions. It explains how to define operator functions as either member or friend functions, detailing the invocation process for unary and binary operators. Additionally, it covers type conversions between incompatible types and the requirements for defining an overloaded casting operator function.

Uploaded by

Pandu Ranga
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

7.

Operator Overloading
and
Type Conversions
We can overload all the C++ operators except the following:
• Class member access operators (., .*).
• Scope resolution operator (::).
• Size operator (sizeof).
• Conditional operator (?:).
The reason why we cannot overload these operators may be
attributed to the fact that these operators take names
(example class name) as their operand instead of values, as
is the case with other normal operators.

Although the semantics of an operator can be extended, we


cannot change its syntax, the grammatical rules that govern
its use such as the number of operands, precedence and
associativity.

To define an additional task to an operator, we must specify


what it means in relation to the class to which the operator is
applied. This is done with the help of a special function,
called operator function, which describes the task. The
general form of an operator function is:
return type classname :: operator op(arglist)
{
Function body // task defined
}
operator op is the function name, where operator is a
keyword.

Operator functions must be either member functions


(nonstatic) or friend functions. A basic difference between
them is that a friend function will have only one argument for
unary operators and two for binary operators, while a
member function has no arguments for unary operators and
only one for binary operators. This is because the object used
to invoke the member function is passed implicitly and
therefore is available for the member function. This is not the
case with friend functions.
The process of overloading involves the following steps:
1. Create a class that defines the data type that is to be used
in the overloading operation.
2. Declare the operator function operator op() in the public
part of the class. It may be either a member function or a
friend function.
3. Define the operator function to implement the required
operations.

Overloaded operator functions can be invoked by expressions


such as
op x or x op for unary operators and
x op y for binary operators.

op x (or x op) would be interpreted as operator op (x) for


friend functions. Similarly, the expression x op y would be
interpreted as either x.operator op (y) in case of member
functions, or operator op (x,y) in case of friend functions.

C3 = C1 + C2; // invokes operator+() function


We know that a member function can be invoked only by an
object of the same class. Here, the object C1 takes the
responsibility of invoking the function and C2 plays the role of
an argument that is passed to the function. The above
invocation statement is equivalent to
C3 = C1.operator+(C2); // usual function call syntax

As a rule, in overloading of binary operators, the left-hand


operand is used to invoke the operator function and the right-
hand operand is passed as an argument.
In case of friend function, the statement C3 = C1 + C2; is
equivalent to C3 = operator+(C1, C2);

There are certain situations where we would like to use a


friend function rather than a member function. For instance,
consider a situation where we need to use two different types
of operands for a binary operator, say, one an object and
another a built-in type data as shown below,
A = B + 2; (or A = B * 2;)
where A and B are objects of the same class. This will work
for a member function but the statement
A = 2 + B; (or A = 2 * B)
will not work. This is because the left-hand operand which is
responsible for invoking the member function should be an
object of the same class. However, friend function allows
both approaches.

We cannot use friend functions to overload certain operators.


(See Table 7.2.) However, member functions can be used to
overload them.

Three types of situations might arise in the data conversion


between uncompatible types:
1. Conversion from basic type to class type.
2. Conversion from class type to basic type.
3. Conversion from one class type to another class type.

C++ allows us to define an overloaded casting operator


function that could be used to convert a class type data to a
basic type.

The casting operator function should satisfy the following


conditions:
• It must be a class member.
• It must not specify a return type.
• It must not have any arguments.

You might also like