C++에서는 함수를 오버로드할 수 있습니다. 그러나 때때로 과부하가 완료되지 않습니다. 이 섹션에서는 함수를 오버로드할 수 없는 다양한 경우를 살펴보겠습니다.
-
함수 시그니처가 같을 때 반환 유형만 다르면 함수를 오버로드할 수 없습니다.
int my_func() {
return 5;
}
char my_func() {
return 'd';
} -
멤버 함수가 클래스에서 동일한 이름과 동일한 매개변수 목록을 가질 경우 오버로드될 수 없습니다.
class My_Class{
static void func(int x) {
//Something
}
void func(int x) {
//something
}
}; -
* 포인터만 다른 매개변수 선언과 배열[]이 같을 때.
int my_func(int *arr) {
//Do something
}
int my_func(int arr[]) {
//do something
} -
상수 또는 휘발성 한정자가 있는 경우에만 다른 매개변수 선언이 동일한 경우.
int my_func(int x) {
//Do something
}
int my_func(const int x) {
//do something
} -
매개변수 선언은 기본 인수만 다를 때 동일합니다.
int my_func(int a, int b) {
//Do something
}
int my_func(int a, int b = 50) {
//do something
}