더북(TheBook)

1.5.4 오버로딩

C++에서 함수는 매개변수 선언이 충분히 다르면 같은 이름을 공유할 수 있다. 이를 함수 오버로딩(Function Overloading)이라고 한다. 먼저 예제를 살펴보자.

#include <iostream>
#include <cmath>

int divide(int a, int b) {
    return a / b;
}

float divide(float a, float b) {
    return std::floor(a / b);
}

int main() {
    int x = 5, y = 2;
    float n = 5.0, m = 2.0;
    std::cout << divide(x, y) << std::endl;
    std::cout << divide(n, m) << std::endl;
    std::cout << divide(x, m) << std::endl;    // 오류: 모호하다.
}
신간 소식 구독하기
뉴스레터에 가입하시고 이메일로 신간 소식을 받아 보세요.