Presentation is loading. Please wait.

Presentation is loading. Please wait.

Operator Overloading Like most languages, C++ supports a set of operators for its built-in types. Example: int x=2+3; // x=5 However, most concepts for.

Similar presentations


Presentation on theme: "Operator Overloading Like most languages, C++ supports a set of operators for its built-in types. Example: int x=2+3; // x=5 However, most concepts for."— Presentation transcript:

1 Operator Overloading Like most languages, C++ supports a set of operators for its built-in types. Example: int x=2+3; // x=5 However, most concepts for which operators are conventionally used are not built-in types Example: Monster x(“flying”); Monster y(“walking”); x + y = ????

2 Operator Overloading Definition: the syntactic possibility that C++ offers to redefine the actions of an operator in a given context Essentially all operators may be overloaded, here is a complete list: + - * / % ^ & | ~ ! , = < > <= >= ++ -- << >> == != && += -= *= /= %= ^= &= <<= >>= [] () -> ->* new || |= delete

3 Operator overloading Some operators can not be overloaded:
. .* :: ?: You can create any new operators

4 Operator Overloading Example
class Vector { public: //… Vector operator-(Vector b)const; double operator*(Vector b)const; }; Vector Vector :: operator-(Vector b) const { return Vector(getX() - b.getX(), getY()-b.getY()); } double Vector :: operator*(Vector b)const { return getX() * b.getX() + getY() * b.getY());

5 Operator Overloading The operator functions are normal functions and can be called explicitly: Vector a(20, 15); Vector b(5, 40); Vector c(5, 40); // Two statements bellow are identical // Compiler translates * and – operators in // expressions involving Vector objects to calls of // the respective functions Vector u = a * (b-c); Vector u = a.operator*(b.operator-c));

6 Operator overloading When to use them
Operator overloading should be used in situations where an operator has a defined action. Person y; Person x = y; // action is obvious Operator overloading should not be used when the action is not desired as it has negative side effects Person x; x=x+y; // action is NOT obvious

7 More examples void Person :: operator=(Person const &other) {
delete name; delete address; delete phone; name = new String(other->name); address = new String(other->address); phone = new String(other->phone); }

8 When to use operator overloading
Operator overloading can be used in situations where the usage of the operator is common and when no ambiguity in the meaning of the operator is introduced by redefining it Operator overloading makes source files more readable. An operator simply does what it is designed to do.

9 Increment and Decrement
The compiler will not automatically transform: v += w; into v = v + w; Instead the operator+= function must be defined

10 Prefix and postfix x++ is not equivalent to x = x + 1
Prefix increment (++x/--x) Fraction & Fraction :: operator++(); Fraction & Fraction :: operator--(); Postfix increment (x++/x--) Fraction Fraction :: operator++(int); Fraction Fraction :: operator--(int); Make sure you are consistent: Once you overload prefix or postfix, user of the class will expect you to have all prefix/postfix operators overloaded

11 Defining type conversion
It is very common to use type conversion: Fraction(int); Automatically converts integer into a fraction Sometimes referred as “creating object on the fly”

12 Defining Type Conversion
Lets consider a type conversion from fractions to floating-point numbers. Since double is not a class, no constructor can be defined for it. Instead, the fraction class must define a type conversion operator: operator double() const;

13 Type conversion example
class Fraction { public: // … operator double() const; }; Fraction f(4,5); Y = sqrt(f); The compiler will actually do the following: Y = sqrt(f.operator double());

14 Overloading operator[]()
We want to have a class which is meant to operate on an array of ints. Indexing the array elements occurs with the standard array operator[], but additionally the class checks for boundary overflow.

15 operator[]() class IntArray { public: int operator[](int index) const;
private: int * data; int size; }; Int main() { IntArray x(20); // Boundary overflow is produced for last element for( int I = 0; I <= 20; I++) cout << x[I] << endl; return 0; }

16 Overload operator= IntArray const & IntArray :: operator=(const IntArray & from) { // Take this action only when there is no auto assignment if ( this != &from) { delete [] data; copy(other); // copies all data } return *this;

17 Overloading operator new
If the operator new is overloaded, it must have a void * return type, and at least an argument of type size_t. The size_t type is defined in stddef.h, which must therefore be included when the operator new is overloaded #include <stddef.h> void * X :: operator new(size_t sizeofX) { void *p = new char[sizeofX]; return (memset(p, 0, sizeof(X))); }

18 Overloading operator delete(void*)
The operator delete must have a void * argument, and an optional second argument of type size_t, which is the size in bytes of objects on the class for which operator delete is overloaded. The return type of operator delete is void. void operator delete(void *); void operator delete(void *, size_t);

19 Overloading delete operator
delete ptr; Can call the destructor function itself X::~X(ptr); Do things with the memory pointed by ptr and possibly known size void X::operator delete(void * ptr) { // whatever else is considered necessary // use default operator delete ::delete ptr; }

20 Operator overloading as functions
Overloaded operator can be function or member function. Sometimes you have to make those functions friends of a class to make them work Whenever possible you should define operator to be a member function, except The type of the first argument of the operator is not a class Vector operator*(double, Vector); The type of the first argument is a class that you have no authority to modify ostream & operator<<(ostream&, vector); Type conversion is desired on the first argument Fraction operator*(Fraction, Fraction); Fraction b = 2 * a; // OK

21 Operator overloading advices
Avoid cryptic interpretations of operators Stack S; S += 5; // DON’T use s.push(5); X = S--; // DON’T use x = s.pop(); Supply a Complete Set A = A + B  A +=B A – B = A + (-B) Minimize type conversions To much type conversions can lead to mistakes

22


Download ppt "Operator Overloading Like most languages, C++ supports a set of operators for its built-in types. Example: int x=2+3; // x=5 However, most concepts for."

Similar presentations


Ads by Google