Functions in C++ that cannot be overloaded

There are many function declarations in C++ that cannot be overloaded. We will look forward to those in this article.

What is Function Overloading?

It is an attribute in C++ where one can define two or more functions with the same name but each one of them should have different parameters. It is also known as method overloading.

Functions that cannot be overloaded in C++

1) Inside a class, if any member function has a static member function declaration, then that member function cannot be overloaded with the same name or even with the same parameter type.
Let us see that with the help of an example below.

  #include<bits/stdc++.h>
using namespace std;
class example
{
    static void examp(int x) {}
    void examp(int x) {}
};
int main()
{
    example e;
}

This program shows an error in compilation because the “examp” function cannot be overloaded.

2) If the parameters of two different functions differ by one being a function type and other being a pointer to the same function type means the same.

void x(char ()); 
void x(char (*)());

3) If the return type of two functions is different, then it cannot be overloaded. Let us see it in the code below.

#include<bits/stdc++.h>
using namespace std;
int fun()
{
    return 20;
}
char fun()
{
    return 'x';
}
int main()
{
    char y=fun();
    return 0;
}

This program shows an error in compilation because the “fun” function cannot be overloaded.

4) Functions having parameters as array [] or pointers is one and the same thing. Let us see it in the code below.

int function(int *p); 
int function(int p[]);

Both of these represent the same thing, thus “function” can’t be overloaded.

5) If arguments of two functions only differ in the default argument type then they are also considered the same. Let us see it in the code below.

#include<bits/stdc++.h>
using namespace std;
int funct(int a,int b)
{
    return a*5;
}
int funct(int a,int b=5)
{
    return a*b;
}
int main()
{
    return 0;
}

This program shows an error in compilation because the “funct” function cannot be overloaded.

6) If the parameter of one function has const or any other volatile qualifier while the other doesn’t have any, it still means the same. It cannot be overloaded and thus will show an error.

#include<bits/stdc++.h>
using namespace std;
int function(int a)
{
    return a;
}
int function(const int a)
{
    return a;
}
int main()
{
    return 0;
}

Thank You for reading!!
I hope it helps!!

Leave a Reply

Your email address will not be published. Required fields are marked *