12.5 정적바인딩과 동적바인딩
정적 바인딩 동적바인딩
1) 정적바인딩이란?
: 컴파일 타임에 호출될 함수가 결정되는 것
2) 동적 바인딘이란?
: 런타임에 호출될 함수가 결정되는 것
차이
1) 정적 바인딩
- 빠르다.
- 호출할 함수를 정해놓아야 하므로 유연성 떨어짐.
2) 동적 바인딩
- 함수포인터를 이용해 어떤 함수를 호출할지를 결정하므로 느리다.
- 유연하다.
최종 코드
#include <iostream>
#include <string>
#include <vector>
int add(const int& inA, const int& inB)
{
std::cout << "더하기 입니다. " << std::endl;
return inA + inB;
}
int subtract(const int& inA, const int& inB)
{
std::cout << "빼기 입니다. " << std::endl;
return inA - inB;
}
int main()
{
int input;
std::cin >> input;
int(*func)(const int&, const int&) = nullptr;
//함수 포인터가 어떤 함수를 호출할지를 결정하는 부분
switch (input)
{
case 0 :
func = add;
break;
case 1 :
func = subtract;
break;
}
std::cout << func(2, 2) << std::endl;
return 0;
}
Author And Source
이 문제에 관하여(12.5 정적바인딩과 동적바인딩), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://velog.io/@kwt0124/12.5-정적바인딩과-동적바인딩저자 귀속: 원작자 정보가 원작자 URL에 포함되어 있으며 저작권은 원작자 소유입니다.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)