-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathgeneric_copy_constructor2.cpp
More file actions
44 lines (34 loc) Β· 1.21 KB
/
Copy pathgeneric_copy_constructor2.cpp
File metadata and controls
44 lines (34 loc) Β· 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/*
* HOME : ecourse.co.kr
* EMAIL : smkang @ codenuri.co.kr
* COURSENAME : C++ Template Programming
* MODULE : generic_copy_constructor2.cpp
* Copyright (C) 2017 CODENURI Inc. All rights reserved.
*/
#include <iostream>
using namespace std;
template<typename T> class complex
{
T re;
T im;
public:
complex(T a = T(), T b = T()) : re(a), im(b) {}
// λ³΅μ¬ μμ±μ λͺ¨μ 1.
//complex(const complex<T>&); // complex<double> c3 = c1; μΌλ c1μ λ°λμ complex<double> μ΄μ΄μΌ νλ€.
// λ³΅μ¬ μμ±μ λͺ¨μ 2.
//complex(const complex<int>&); // complex<double> c3 = c1; μΌλ c1μ λ°λμ complex<int> μ΄μ΄μΌ νλ€.
// λ³΅μ¬ μμ±μ λͺ¨μ 3. μΌλ°νλ λ³΅μ¬ μμ±μ
// "Uκ° Tλ‘ λ³΅μ¬ κ°λ₯νλ€λ©΄ complex<U> λ complex<T> λ‘ λ³΅μ¬ κ°λ₯ν΄μΌ νλ€."
template<typename U> complex(const complex<U>&); // complex<double> c3 = c1; μΌλ c1μ complex<U> μ΄λ―λ‘, μμμ νμ
μ complex μ΄λ€.
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, 2); // ok
complex<int> c2 = c1; // ok. λ³΅μ¬ μμ±μ
complex<double> c3 = c1; // error. c1, c3 λ λ€λ₯Έ νμ
μ΄λ€.!
}