[열혈 c++ 프로그래밍] ch13

hyng·2023년 3월 27일
0

열혈 c++ 프로그래밍

목록 보기
12/12

열혈 c++ 프로그래밍을 보고 요약정리합니다.

12장은 생략

  • 함수 템플릿
template <typename T>
T Add(T num1, T num2)
{
    return num1 + num2;
}

int main(void) {
    cout << Add<int> (15, 20) << endl; //Add(15, 20)
    cout << Add<double> (2.9, 3.7) << endl; //Add(2.9, 3.7)
    cout << Add<int> (3.2, 3.2) << endl; //Add(3.2, 3.2)
    cout << Add<double> (3.14, 2.75) << endl; //Add(3.14, 2.75)
    return 0;
}
  • 템플릿 함수와 일반 함수 모두 구현할 수 있지만, 일반 함수 호출이 우선시된다.
  • 만약 템플릿 함수를 호출하고자 한다면 <> 표시를 통해 호출을 명시해야 한다.
    #include <iostream>
    using namespace std;
    
    template <typename T>
    T Add(T num1, T num2)
    {
    	cout << "T Add(T num1, T num2)" << endl;
    	return num1 + num2;
    }
    int Add(int num1, int num2)
    {
      cout << "Add(int num1, int num2)" << endl;
    	return num1 + num2;
    }
    double Add(double num1, double num2)
    {
    	cout << "Add(double num1, double num2)" << endl;
      return num1 + num2;
    }
    
    int main(void)
    {
    	cout << Add(5, 7) << endl;
    	cout << Add(3.7, 7.5) << endl;
    	cout << Add<int>(5, 7) << endl;
    	cout << Add<double> (3.7, 7.5) << endl;
    	return 0;
    }
  • 함수 템플릿 특수화
    • 다음의 코드는 컴파일러에게 char* 형 템플릿 함수가 필요한 경우에는 별도로 만들지 말고 해당 코드를 사용하라는 의미

      temlate <>
      char * Max<char*>(char * a, char * b)
      {
      	cout << "char* Max<char*>(char* a, char* b)" << endl;
      	return strlen(a) > strlen(b) ? a : b;
      }
  • 클래스 템플릿
    template <typename T>
    class Point
    {
    private:
        T xpos, ypos;
    public:
        Point(T x = 0, T y = 0) : xpos(x), ypos(y)
        {}
        void ShowPosition() const
        {
            cout << '[' << xpos << ", " << ypos << ']' << endl;
        }
    };
    
    int main(void) {
        Point<int> pos1(3, 4);
        pos1.ShowPosition();
        
        Point<double> pos2(2.4, 3.6);
        pos2.ShowPosition();
        
        Point<char> pos3('P', 'F');
        pos3.ShowPosition();
        return 0;
    }
  • 클래스 템플릿을 헤더파일과 소스파일로 나누면 컴파일 에러가 발생한다.
    //PointTemplate.h
    
    #infdef __POINT_TEMPLATE_H_
    #define __POINT_TEMPLATE_H_
    
    template <typename T>
    class Point
    {
    	private:
    		T xpos,ypos;
    	public:
    		Point(T x=0, T y=0);
    		void ShowPosition() const;
    };
    #endif
    
    //PointTemplate.cpp
    #include<iostream>
    #include "PointTemplate.h"
    using namespace std;
    
    template <typename T>
    Point<T>::Point(T x, T y) : xpos(x), ypos(y)
    {}
    
    template <typename T>
    void Point<T>:: ShowPosition() const
    {
    	cout << '[' << xpos << ", " << ypos << ']' << endl;
    }
    
    //PointMain.cpp
    int main(void) {
        Point<int> pos1(3, 4);
        pos1.ShowPosition();
    
        Point<double> pos2(2.4, 3.6);
        pos2.ShowPosition();
    
        Point<char> pos3('P', 'F');
        pos3.ShowPosition();
        return 0;
    }
    • 컴파일은 파일 단위로 이뤄진다. 그리고 main 함수를 보면 Point'<'int'>' pos1(3, 4)'; 에 Point'<'int'>' 템플릿 클래스를 만들고 Point'<'double'>' pos2(2.4, 3.6);에 Point'<'double'>' 템플릿 클래스를 만들어야 한다.
    • 이를 위해서는 클래스 템플릿인 Point의 모든 것을 알아야 한다. 즉 컴파일러에는 헤더파일뿐만 아니라 소스 코드 정보도 필요하다.
    • 기본적인 해결책은 헤더파일과 소스코드를 하나의 파일에 넣는 것이나 이것이 싫다면 소스 코드에 #include “PointTemplate.cpp” 도 추가해주어야 한다.
profile
공부하고 알게 된 내용을 기록하는 블로그

0개의 댓글