I l@ve RuBoard Previous Section Next Section

29.5 Overloading the ( ) Operator

The ( ) operator can be overloaded for a class to give the class a "default" function. For example:

class example {
    public:
        int operator (  ) (int i) {
            return (i * 2);
        }
};
// ....
     example example_var;

     j = example_var(3);    // j is assigned the value 6 (3 * 2)

Overloading the ( ) operator is rarely done. Normal member functions can easily be used for the same purpose but have the advantage of providing the user with a function name.

    I l@ve RuBoard Previous Section Next Section