0% found this document useful (0 votes)
34 views10 pages

3.4 Explicit, Mutable

Uploaded by

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

3.4 Explicit, Mutable

Uploaded by

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

Pitfalls of Operator Overloading and

Conversion

--Prof. S.N.Shelke
Pitfalls of Operator Overloading and Conversion
 here’s a real danger that can be hard to foresee

 not all operators can be overloaded

 – member access (“.”)

 – pointer to member function invocation (“.*”)

 – conditional expressions (?:)

 – scope identification (::)

 overloading can break C’s duality

 – pointer-like objects and array-like objects not necessarily equal

 – pointer vs. array

• array[10]
• *(array+10)
Pitfalls of Operator Overloading
 You cannot add new operators in the language.

 You cannot change arguments/operands count of any operator.

 You cannot change precedence and associativity of any operator.

 Certain operators cannot be overloaded as friend function (), ->, =, [].

 Certain operators cannot be overloaded ? ,:, ::, .,.*, typeid, typecasting


operators. Overloading these operators will make c++ programs inconsistent
and result in abnormal behavior.

 Many operators are built in language features and does not make any sense
of overloading.

 Operator overloading is made for extending meaning of operator so that it


can be used for objects of user defined class; not for changing their
meanings.
Keyword: explicit
#include <iostream>
using namespace std;
class Complex
{
private:
double real;
double imag;
public:
// Default constructor
Complex(double r = 0.0, double i = 0.0) : real(r), imag(i) {}

// A method to compare two Complex numbers Output:


bool operator == (Complex rhs) {
return (real == rhs.real && imag == rhs.imag)? true : false;
}
Same
};
int main()
{
// a Complex object
Complex com1(3.0, 0.0);

if (com1 == 3.0)
cout << "Same";
else
cout << "Not Same";
return 0;
}
Keyword: explicit (cont…)
#include <iostream>
using namespace std;
class Complex
{
private:
double real;
double imag;
public:
// Default constructor
explicit Complex(double r = 0.0, double i = 0.0) : real(r), imag(i) {}

// A method to compare two Complex numbers


bool operator == (Complex rhs) {
return (real == rhs.real && imag == rhs.imag)? true : false;
}
};
int main()
{
// a Complex object
Complex com1(3.0, 0.0); Output:

if (com1 == 3.0) Compiler Error:


cout << "Same";
else
no match for 'operator==' in 'com1 == 3.0e+0'
cout << "Not Same";
return 0;
}
Keyword: explicit (cont…)
#include <iostream>
using namespace std;
class Complex
{
private:
double real;
double imag;
public:
// Default constructor
explicit Complex(double r = 0.0, double i = 0.0) : real(r), imag(i) {}

// A method to compare two Complex numbers Output:


bool operator == (Complex rhs) {
return (real == rhs.real && imag == rhs.imag)? true : false;
}
Same
};
int main()
{
// a Complex object
Complex com1(3.0, 0.0);

if (com1 == (Complex) 3.0)


cout << "Same";
else
cout << "Not Same";
return 0;
}
Keyword: mutable

Output:
20
Keyword: mutable (cont…)

Example:
Prof. S. N. Shelke
(Assistant Professor)
Department of Computer
Engineering
Sinhgad Academy of Engineering,
Kondhwa, Pune

You might also like