반응형

아래의 Complex 멤버의 data type의 예

  • int로 할 경우 : 실수를 담을 수 없음
  • double로 할 경우 : 실수와 정수를 모두 담을 수 있지만 double은 int보다 overhead가 있음
  • 라이브러리 설계자는 Complex의 틀(template)만 제공하고 내부 타입은 사용자가 결정할 수 있도록 제공
template<typename T>
class Complex
{
    T re, im;
public:
    Complex(T r, T i) : re(r), im(i) {}
};

int main()
{
    Complex<int> c1(1, 2);
    Complex<double> c2(1.1, 2.2);
}
반응형

+ Recent posts