Functions that can’t be overloaded in C++

C++Server Side ProgrammingProgramming

In C++, we can overload the functions. But sometimes the overloading is not done. In this section, we will see what are the different cases, in which we cannot overload the functions.

  • When function signatures are same, only the return type is different, then we cannot overload the function.

int my_func() {
   return 5;
}
char my_func() {
   return 'd';
}
  • When the member functions have the same name and same parameter list in a class, then they cannot be overloaded.

class My_Class{
   static void func(int x) {
      //Something
   }
   void func(int x) {
      //something
   }
};
  • When the parameter declaration, that differs only in a pointer * and an array [] are same.

int my_func(int *arr) {
   //Do something
}
int my_func(int arr[]) {
   //do something
}
  • When the parameter declaration, that differs only in a presence of constant or volatile qualifier are same.

int my_func(int x) {
   //Do something
}
int my_func(const int x) {
   //do something
}
  • When the parameter declaration, that differs only in their default arguments are equivalent.

int my_func(int a, int b) {
   //Do something
}
int my_func(int a, int b = 50) {
   //do something
}
raja
Published on 02-May-2019 08:34:21
Advertisements