3.4 Explicit, Mutable
3.4 Explicit, Mutable
Conversion
--Prof. S.N.Shelke
Pitfalls of Operator Overloading and Conversion
here’s a real danger that can be hard to foresee
• array[10]
• *(array+10)
Pitfalls of Operator Overloading
You cannot add new operators in the language.
Many operators are built in language features and does not make any sense
of overloading.
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) {}
Output:
20
Keyword: mutable (cont…)
Example:
Prof. S. N. Shelke
(Assistant Professor)
Department of Computer
Engineering
Sinhgad Academy of Engineering,
Kondhwa, Pune