In C++, function overloading is a powerful feature that allows multiple functions with the same name to exist, provided they differ in their parameter lists. However, certain scenarios prevent functions from being overloaded due to ambiguity or compiler constraints. Here are the cases where function overloading is not allowed:
Functions with Different Return Types
Functions that differ only in their return type cannot be overloaded. The compiler cannot resolve which function to invoke based solely on the return type.
Pointer vs. Array Parameters
Functions that only differ in whether their parameters are pointers or arrays cannot be overloaded. For example, a function taking an array as an argument is treated the same as a function taking a pointer, leading to ambiguity.
Function Type vs. Pointer to Function
Functions cannot be overloaded if the distinction between their parameters lies in whether they are a function type or a pointer to the same function type.
1. Functions with different return type cannot be overloaded.
2. Members functions declared with samename but one with static function will not be overloaded
3. Parameter declaration that only differ with pointer taking as arguments and array taking as argument that functions cannot be overloaded.
4. A pointer declaration where the only difference lies in whether it is for a function type or a pointer to the same function.
5.Parameter declaration where the only distinction is the presence or absence of const or volatile.
example
#include<iostream>
int main()
{
int func();
float func();
}
2. members functions declared with samename but one with static function will not be overloaded
#include<iostream>
int main()
{
int func();
static int func()
}
3.parameter declaration that only differ with pointer taking as arguments and array taking as argument that functions cannot be overloaded.
#include<iostream>
int main()
{
void func(int *ptr); // function taking a pointer
void func(int arr[]); // function taking an array (error: cannot overload)
}
#include<iostream>
int main()
{
void func(int ())
void func(int(* )())
}
#include<iostream>
int main()
{
void func(int a)
void func(cons int a)
}
Must Read: STM32 ADC: Analog Sensor Reading
Indian Institute of Embedded Systems – IIES