C++/Class
2021. 3. 20. 17:47
동적 바인딩 (Dynamic Binding)
Late Binding
정적 바인딩보다 느리다.
- 대신 프로그래밍이 좀 더 유연해진다.
런타임에 주소를 쫓아가서 함수를 실행시키는 경우이다.
예제
#include <iostream>
int add(int x, int y)
{
return (x + y);
}
int subtract(int x, int y)
{
return (x - y);
}
int multiply(int x, int y)
{
return (x * y);
}
int main()
{
using namespace std;
int x, y;
cin >> x >> y;
int op;
cout << "0 : add, 1 : subtract, 2 : multiply\n";
cin >> op;
int(*func_ptr)(int, int) = nullptr;
switch (op)
{
case 0: func_ptr = add; break;
case 1: func_ptr = subtract; break;
case 2: func_ptr = multiply; break;
}
cout << func_ptr(x, y) << endl;
}
'C++ > Class' 카테고리의 다른 글
C++ 순수 가상 함수 (Pure Virtual Function) (0) | 2021.03.21 |
---|---|
C++ 가상 (함수) 테이블 (Virtual Tables) (0) | 2021.03.20 |
C++ 정적 바인딩 (Static Binding) (0) | 2021.03.20 |
C++ 가상 소멸자 (0) | 2021.03.20 |
C++ 공변 반환형(Covariant Return Type) (0) | 2021.03.20 |