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;
}

좋은 웹페이지 즐겨찾기