Basics of overloading in c++

1) Functions can be overloaded but not classes.
2) Function overloading is not possible for the following scenarios:
·         Differs only in return type
For example: void function(int x);
                    int function(int x);
-because return type is not part of the signature. Signature of a function comprises of Function name, Number of Arguments and Type of Arguments.
·         Differs in static and non-static member functions
For example: static void function(int x);
                           void function(int x);
·         Array and pointer declaration
For example:  void function(int *ptr);
void function(int array[]);
-because an array is implicitly converted to a pointer.
3) When the operator does not modify its operands, the best way to overload the operator is via friend function. If the operators (such as increment, decrement and dereference) modify the operands then they should be members of the class.
4) Operators that cannot be overloaded are:
·         Member – a.b
·         Pointer to member by reference – a.*b
·         Ternary conditional – a?b:c
·         Scope resolution – a::b
·         Pointer to member – a::*b
·         Sizeof – sizeof()
·         Type identification - typeid(object)
·         Cast operators – dynamic_cast, 
                            static_cast, 
                            const_cast and 
                            reinterpret_cast

0 comments:

Post a Comment