반응형
    
    
    
  일반화된 복사 생성자
- 
클래스 템플릿의 멤버 함수 템플릿으로 구현하는 복사 생성자. 
- 
U(int)가 T(double)로 복사(대입) 가능하다면 Complex<U>도 Complex<T>에 복사(대입) 가능해야 한다. 
- 
friend class로 선언되어야 한다. 
template<typename T> class Complex
{
    T re, im;
public:
    Complex(T a = {}, T b = {}) : re(a), im(b) {}
    T getReal() const;
    static int cnt;
    template<typename U> T func(const U& c);
    // 일반화된 복사 생성자 선언
    template<typename U>
    Complex(const Complex<U>& c);
    template<typename> friend class Complex;
};
// 일반화된 복사 생성자 구현부
template<typename T> template<typename U>
Complex<T>::Complex(const Complex<U>& c)
    : re(c.re), im(c.im)
{
}
int main()
{
    Complex<int> c1(1, 1);
    Complex<int> c2 = c1;
    Complex<double> c3 = c1;
}반응형
    
    
    
  '프로그래밍 언어 > C++' 카테고리의 다른 글
| c++ 템플릿 typename (0) | 2020.12.31 | 
|---|---|
| C++ 템플릿 template과 friend (0) | 2020.12.31 | 
| C++ 템플릿 클래스 템플릿(Class template) (0) | 2020.12.31 | 
| C++ 템플릿 타입 추론(Argument Decay) (0) | 2019.07.14 | 
| C++ 템플릿 타입 추론(배열의 주소) (0) | 2019.07.14 |