Topic : Operator Overloading
Author : Danny Kalev
Page : << Previous 2  Next >>
Go to page :


operators can be overloaded both in their unary and binary forms:

+    -    *     &

Overloaded operators are inherited in the same manner as other base class functions. Note that these rules do not apply to the assignment operator, which is implicitly declared for a class if it is not declared by the user. Therefore, a base class assignment operator is always hidden by the copy assignment operator of the derived class. (The assignment operator is discussed in Chapter 4, "Special Member Functions: Default Constructor, Copy Constructor, Destructor, and Assignment Operator.")

Operators Can Only Be Overloaded for User-Defined Types
An overloaded operator must take at least one argument of a user-defined type (operators new and delete are an exception -- see Chapter 11, "Memory Management," for more details). This rule ensures that users cannot alter the meaning of expressions that contain only fundamental types. For example

int i,j,k;
k = i + j; //always uses built-in = and +



Invention of New Operators Is Not Allowed
An overloaded operator extends a built-in one, so you cannot introduce new operators into the language (conversion operators differ from ordinary overloaded operators in this respect). The following example attempts to overload the @ operator, but does not compile for that reason:

void operator @ (int); //illegal, @ is not a built-in operator or a type name

Precedence and Argument Number
Neither the precedence nor the number of arguments of an operator can be altered. For example, an overloaded && must have exactly two arguments -- as does the built-in && operator. In addition, the precedence of an operator cannot be altered when it is overloaded. A sequence of two or more overloaded operators, for instance t2<t1/t2, is evaluated according to the precedence rules of the built-in operators. Because the division operator ranks higher than the less operator, the expression is always interpreted as t2<(t1/t2).

Default Parameters
Unlike ordinary functions, overloaded operators cannot declare a parameter with a default value (operator () is the exception to this rule; it is discussed later).

class Date
{
private:
  int day;
  int month;
  int year;
public:
  Date & operator += (const Date & d = Date() ); //error, default argument
};


This rule might seem arbitrary, but it captures the behavior of built-in operators, which never have default operands either.

Operators That Cannot Be Overloaded
There are several operators that cannot be overloaded. They are characterized by the fact that they take a name, rather than an object, as their right operand. These operators are:

Direct member access, operator .

Deference pointer to class member, operator .*


Scope resolution, operator ::

Size of, operator sizeof

The conditional operator ?: cannot be overloaded either.

Additionally, the new type cast operators -- static_cast<>, dynamic_cast<>, reinterpret_cast<>, and const_cast<> -- and the # and ## preprocessor tokens cannot be overloaded.

Conversion Operators
It is not uncommon to find C++ and C code used together. For instance, legacy systems that were originally written in C can be wrapped by an object-oriented interface. Such bilingual systems often need to support a dual interface at the same time -- one that caters to an object-oriented environment and another that caters to C environment. Classes that implement specific numeric entities -- such as complex numbers and binary numbers -- and nibbles also tend to use conversion operators to enable smoother interaction with fundamental types.

Strings are a classic example of the need for a dual interface. A string object might have to be used in contexts that support only null-terminated char arrays. For example

class Mystring
{
private:
  char *s;
  int size;
public:
  Mystring(const char *);
  Mystring();
//...
};
#include <cstring>
#include "Mystring.h"
using namespace std;
int main()
{
  Mystring str("hello world");
int n = strcmp(str, "Hello");  //compile time error:
                                 //str is not of  type const char *
  return 0;
}


C++ offers an automatic means of type conversion for such cases. A conversion operator can be thought of as a user-defined typecasting operator; it converts its object to a different type in contexts that require that specific type. The conversion is done automatically. For example

class Mystring   //now with conversion operator
{
private:
  char *s;
  int size;
public:
  Mystring();
  operator const char * () {return s; } //convert Mystring  to a C-string
//...
};
int n = strcmp(str, "Hello"); //OK, automatic conversion of str to const char *


Conversion operators differ from ordinary overloaded operators in two ways. First, a conversion operator does not have a return value (not even void). The return value is deduced from the operator's name.

Secondly, a conversion operator takes no arguments.

Conversion operators can convert their object to any given type, fundamental and user-defined alike:

struct DateRep  //legacy C code
{
  char day;
  char month;
  short year;
};
class Date // object-oriented wrapper
{
private:
  DateRep dr;
public:
  operator DateRep () const { return dr;} // automatic conversion to DateRep
};
extern "C" int transmit_date(DateRep);  // C-based communication API function
int main()
{
  Date d;
  //...use d
  //transmit date object as a binary stream to a remote client
  int ret_stat = transmit_date; //using legacy communication API
  return 0;
}


Standard Versus User-Defined Conversions
The interaction of a user-defined conversion with a standard conversion can cause undesirable surprises and side effects, and therefore must be used with caution. Examine the following concrete example.

A non-explicit constructor that takes a single argument is also a conversion operator, which casts its argument to an object of this class. When the compiler has to resolve an overloaded function call, it takes into consideration such user-defined conversions in addition to the standard ones. For example

class Numeric
{
private:
  float f;
public:
  Numeric(float ff): f(ff) {} //constructor is also a float-to-Numeric
                              // conversion operator
};
void f(Numeric);
Numeric num(0.05);
f(5.f);  //OK, calls void f(Numeric). Numeric's constructor
         //converts argument to a Numeric object


'Suppose you add, at a later stage, another overloaded version of f():

void f (double);

Now the same function call resolves differently:

f(5.f); // now calls f(double), not f(Numeric)

This is because float is promoted to double automatically in order to match an overloaded function signature. This is a standard type conversion. On the other hand, the conversion of float to Numeric is a user-defined conversion. User-defined conversions rank lower than standard conversions -in overload resolution; as a result, the function call resolves differently.

Because of this phenomenon and others, conversion operators have been severely criticized. Some programming schools ban their usage altogether. However, conversion operators are a valuable -- and sometimes inevitable -- tool for bridging between dual interfaces, as you have seen.

Postfix and Prefix Operators
For primitive types, C++ distinguishes between ++x; and x++; as well as between --x; and x--;. Under some circumstances, objects have to distinguish between prefix and postfix overloaded operators as well (for example, as an optimization measure. See Chapter 12, "Optimizing Your Code"). Postfix operators are declared with a dummy int argument, whereas their prefix counterparts take no arguments. For example

class Date
{
public:
  Date& operator++(); //prefix
  Date& operator--(); //prefix
  Date& operator++(int unused); //postfix
  Date& operator--(int unused); //postfix
};
void f()
{
Date d, d1;
d1 = ++d;//prefix: first increment d and then assign to d1
d1 = d++; //postfix; first assign, increment d afterwards
}


Using Function Call Syntax
An overloaded operator call is merely "syntactic sugar" for an ordinary function call. You can use the explicit function call instead of the operator syntax as follows:

bool operator==(const Date& d1, const Date& d2);
void f()
{
  Date d, d1;
  bool equal;
  d1.operator++(0); // equivalent to: d1++;
  d1.operator++(); // equivalent to: ++d1;
  equal = operator==(d, d1);// equivalent to: d==d1;
  Date&(Date::*pmf) (); //pointer to member function
  pmf = & Date::operator++;
}


Consistent Operator Overloading
Whenever you overload operators such as + or -, it might become necessary to support the corresponding += and -= operators as well. The compiler does not do that for you automatically. Consider the following example:

class Date
{
public:
  Date& operator + (const Date& d);  //note: operator += not defined
};
Date d1, d2;
d1 = d1 + d2; //fine; uses overloaded + and default assignment operator
d1 += d2; //compile time error: 'no user defined operator += for class Date'


Theoretically, the compiler could synthesize a compound operator += by combing the assignment operator and the overloaded + operator so that the expression d1 += d2; is automatically expanded into d1 = d1+d2;. However, this is undesirable because the automatic expansion might be less efficient than a user-defined version of the operator. An automated version creates a temporary object, whereas a user-defined version can avoid it. Moreover, it is not difficult to think of situations in which a class has an overloaded operator +, but

Page : << Previous 2  Next >>