Operator Overloading  «Prev  Next»
Lesson 7Unary and binary operator overloading
ObjectiveDifference between overloading Unary and Binary Operators

Difference between overloading Unary and Binary Operators in C++

Unary and binary operators can be overloaded as nonstatic member functions. Implicitly they are acting on a class value.

C++ Nonstatic Member Functions

Data members can be declared with the storage class modifier static. A data member that is declared static is shared by all variables of that class and is stored uniquely in one place. Since a static member is independent of any particular instance, it can be accessed in the form

static Storage Class

The static storage class instructs the compiler to keep a local variable in existence during the life-time of the program instead of creating and destroying it each time it enters and leaves scope. Therefore, making local variables static allows them to maintain their values between function calls.
The static modifier may also be applied to global variables. When this is done, it causes that variable's scope to be restricted to the file in which it is declared.
In C++, when static is used on a class data member, it causes only one copy of that member to be shared by all objects of its class.

class name :: identifier 

Non-static members in C++

Nonstatic members are created for each instance of the class.
The
  1. assignment,
  2. function call,
  3. subscripting, and
  4. class pointer
operators can be overloaded only by nonstatic member functions.
Pointers to members (data and functions) work differently from other pointers. The syntax for declaring a pointer to a nonstatic data member or a nonstatic member function requires a class name and scope operator before the asterisk. Pointers to members can never be cast to ordinary pointers, and vice versa. You cannot declare a reference to a member. A pointer to a static member is an ordinary pointer, not a member pointer. The following are some simple examples of member pointers:

struct simple {
 int data;
 int func(int);
};
int simple::* p = &simple::data;
int (simple::*fp)(int) = &simple::func;
simple s;
s.*p = (s.*fp)(42);


Unary operators can be overloaded as ordinary functions that take a single argument of class or reference to class type.
Binary operators can be overloaded as ordinary functions that take one or both arguments of class or reference to class type. In the next several lessons, we will look closely at overloading both unary and binary operators. Take a look at the following unary operator overloading example, in this case the unary operators increment (++) and decrement (--):

//Increment and decrement overloading
class Inc {
 private:
 int count ;
 public:
 Inc() {
  //Default constructor
  count = 0 ;
 }
 Inc(int C) {
  // Constructor with Argument
  count = C ;
 }
 Inc operator ++ () {
  // Operator Function Definition
  return Inc(++count);
 }
 Inc operator -- () {
  // Operator Function Definition
  return Inc(--count);
 }
 void display(void) {
  cout << count <<  endl ;
 }
};

void main(void) {
 Inc a, b(4), c, d, e(1), f(4);

 cout <<"Before using the operator ++()\n";
 cout << "a = ";
 a.display();
 cout << "b = ";
 b.display();

 ++a;
 b++;

 cout << "After using the operator ++()\n";
 cout << "a = ";
 a.display();
 cout << "b = ";
 b.display();

 c = ++a;
 d = b++;

 cout << "Result prefix (on a) and postfix (on b)\n";
 cout << "c = ";
 c.display();
 cout << "d = ";
 d.display();

 cout << "Before using the operator --()\n";
 cout << "e = ";
 e.display();
 cout << "f = ";
 f.display();

 --e;
 f--;

 cout << "After using the operator --()\n";
 cout << "e = ";
 e.display();
 cout << "f = ";
 f.display();
}