Print service provided by iDogiCat: http://www.idogicat.com/
home logo





Home > IT > Programming > C++ Programming > iDog's Interview C++ > iDog's Interview C++ Part 3

iDog's Interview C++ Part 3

Overloaded operators

Overloaded increment/decrement operator


const type& operator <operator> ();    // prefix op
type operator <operator> (int);  // postfix op
// where <operator> is ++ or --

How can the compiler differentiate between prefix and postfix operator? By convention, an integer variable is supplied as a dummy parameter to the operator declaration. The parameter's value is ignored; it is just a signal that this is the postfix operator.


// prefix ++ operator
const MyClass& MyClass::operator++()
{
    // do increment operation...

    return *this;
}

// postfix ++ operator
MyClass MyClass::operator++(int)  // return value, not reference
{
    MyClass temp(*this);
    // do increment operation...

    return temp;
}

Binary operator

+, -, *, /, %


type operator <operator> (const type& rhs);

// example
Integer opertor+(const Integer& integer);

Conversion operator

Conversion operators solve the following problem:


<var of basic type> = <instance of a class>;
Conversion operators don't have return types although they do return values:

MyClass::operator type() {
    ...
    return xxx; // xxx is of type 'type'
}

Assignment Operator


<i>type</i>& operator=(const type& rhs);


class String
{
private:
    char* m_pszData;

public:
    String();
    String(const String&);
    ~String();
    //...
    String& operator=(const String& rhs);
};

String& String::operator=(const String& rhs)
{
    if(this != &rhs) {
        delete[] m_pszData;
        m_pszData = new char[strlen(rhs.m_pszData) + 1];
        strcpy(m_pszData, rhs.m_pszData);
    }

    return *this;
}

Overloaded Operators: Member function or not?

Usually overloaded operators can be either member functions or friend functions. A member operator function always provides the left argument as the pointer this. If we need an operator function that takes the 'this' object as the right side argument, we should implement it as an ordinary function (if it doesn't need to access the private data of the class) or a friend function (if it does).

But there are four operators which must be member functions:

  • assignment operator: =
  • subscript operator: []
  • function call operator: ()
  • indirection operator: ->

Nonredefinable operators:

  • . - member selection
  • .* - pointer-to-member selection
  • :: - scope resolution
  • ?: - conditional
  • sizeof
  • # - preprocessor convert to string
  • ## - preprocessor concatenate

An example of pointer-to-member selection operator:


class MyClass {
public:
    int myfunc(int);
};

MyClass mc;
MyClass* pmc = &mc;

int (MyClass::*pFunc)(int) = &(MyClass::myfunc);
mc.*pFunc(1);
pmc->*pFunc(2);

Limitations on Operator Overloading

  • operators on build-in types (int, char, ...) cannot be overloaded
  • the precedance order cannot be changed
  • the arity (i.e. how many terms are used in the operator) cannot be changed
  • new operators cannot be introduced in

Default things supplied by compiler to class

The compiler supplies a class 4 things if you don't define them explicately:

  • default constructor with no parameter
  • default copy constructor
  • default destructor
  • default assignment operator