회원 로그인
|
![](http://dvframes.com/rb/_var/simbol/artsone.jpg)
Templete (템플릿)
C++ Templete (템플릿)
[템플릿 클래스]
탬플릿 클래스란 클래스의 멤버 속성과 메소드들의 타입을 일반화한 클래스를 말한다.
[템플릿 문법]
template <typename T>
class 템플릿클래스 {
private:
T 템플릿변수
public:
T 템플릿메소드(T 매개변수)
};
[제네릭 문법]
public class 제네릭클래스<T> {
private T 제네릭변수 = default(T);
public T 제네릭메소드(T 매개변수)
}
// 제네릭클래스<T> 제네릭객체인스턴스변수 = new 제네릭클래스<T>();
template <typename T>
T 템플릿클래스<T>::템플릿메소드(T 매개변수)
// 템플릿클래스<T>* 템플릿객체인스턴스 = new 템플릿클래스<T>()
[템플릿 클래스]
탬플릿 클래스란 클래스의 멤버 속성과 메소드들의 타입을 일반화한 클래스를 말한다.
[템플릿 문법]
template <typename T>
class 템플릿클래스 {
private:
T 템플릿변수
public:
T 템플릿메소드(T 매개변수)
};
[제네릭 문법]
public class 제네릭클래스<T> {
private T 제네릭변수 = default(T);
public T 제네릭메소드(T 매개변수)
}
// 제네릭클래스<T> 제네릭객체인스턴스변수 = new 제네릭클래스<T>();
template <typename T>
T 템플릿클래스<T>::템플릿메소드(T 매개변수)
// 템플릿클래스<T>* 템플릿객체인스턴스 = new 템플릿클래스<T>()
#include <iostream> #include <string> using namespace std; // 일반 업/다운캐스팅 class NormalDataContainer { private: int iValue; double dValue; char cValue; string sValue; public: void SetIntValue(int value); int GetIntValue(); void SetDoubleValue(double value); double GetDoubleValue(); void SetCharValue(char value); char GetCharValue(); void SetStringValue(string value); string GetStringValue(); }; // 기본 입출력(Set, Get) void NormalDataContainer::SetIntValue(int value) { iValue = value; } int NormalDataContainer::GetIntValue() { return iValue; } void NormalDataContainer::SetDoubleValue(double value) { dValue = value; } double NormalDataContainer::GetDoubleValue() { return dValue; } void NormalDataContainer::SetCharValue(char value) { cValue = value; } char NormalDataContainer::GetCharValue() { return cValue; } void NormalDataContainer::SetStringValue(string value) { sValue = value; } string NormalDataContainer::GetStringValue() { return sValue; } // 몬스터 클래스-템플릿 예제 추가 class CMonster { private: int damage; float speed; public: CMonster() { this->damage = 20; this->speed = 7; } void Move() { cout << "몬스터가 " << speed << "로 이동합니다." << endl; } void Attack() { cout << "몬스터가 " << damage << "로 공격을 합니다." << endl; } }; // 템플릿 // 기본 입출력에서 형이 달라 형에 맞춰 입출력해야 하는데 // 템플릿은 같은 함수에 무슨 형이 들어오는지 명시하면 그 형에 맞게 출력 template <typename Type> // 템플릿 타입 생성 class TemplateDataContainer { private: Type value; public: ~TemplateDataContainer() { delete value; } void SetValue(Type value); Type GetValue(); }; // 템플릿 입출력 template <typename Type> void TemplateDataContainer<Type>::SetValue(Type value) { this->value = value; } template <typename Type> Type TemplateDataContainer<Type>::GetValue() { return value; } void main() { // 기본 타입 NormalDataContainer* contain1 = new NormalDataContainer(); contain1->SetIntValue(10); cout << "Normal Contain object int value : " << contain1->GetIntValue() << endl; delete contain1; NormalDataContainer* contain2 = new NormalDataContainer(); contain2->SetDoubleValue(10.123); cout << "Normal Contain object double value : " << contain2->GetDoubleValue() << endl; delete contain2; NormalDataContainer* contain3 = new NormalDataContainer(); contain3->SetCharValue('B'); cout << "Normal Contain object char value : " << contain3->GetCharValue() << endl; delete contain3; NormalDataContainer* contain4 = new NormalDataContainer(); contain4->SetStringValue("문자열"); cout << "Normal Contain object string value : " << contain4->GetStringValue() << endl; delete contain4; // 템플릿 타입 TemplateDataContainer<int*>* contain5 = new TemplateDataContainer<int*>(); contain5->SetValue(new int(10)); cout << "Template Contain object int value : " << contain5->GetValue() << endl; delete contain5; TemplateDataContainer<double*>* contain6 = new TemplateDataContainer<double*>(); contain6->SetValue(new double(100.123)); cout << "Template Contain object double value : " << contain6->GetValue() << endl; delete contain6; TemplateDataContainer<char*>* contain7 = new TemplateDataContainer<char*>(); contain7->SetValue(new char('C')); cout << "Template Contain object char value : " << *(contain7->GetValue()) << endl; delete contain7; TemplateDataContainer<string*>* contain8 = new TemplateDataContainer<string*>(); contain8->SetValue(new string("나도문자열")); cout << "Template Contain object string value : " << contain8->GetValue()->c_str() << endl; delete contain8; // 해제 꼭 필요 // 몬스터 클래스-템플릿 예제 추가 // 몬스터의 동적 객체를 저장할 템플릿 컨테이너 객체 생성 TemplateDataContainer<CMonster*>* contain9 = new TemplateDataContainer<CMonster*>(); // 몬스터 객체 생성 CMonster* monster = new CMonster(); // 컨테이너에 몬스터 객체 저장 contain9->SetValue(monster); // 컨테이너에서 몬스터 추출 CMonster* m = contain9->GetValue(); // 몬스터 이동 m->Move(); // 몬스터 공격 contain9->GetValue()->Attack(); delete contain9; }
![](/rb/modules/bbs/theme/_pc/list01/image/ico_comment.gif)
285개(1/15페이지)
번호 | 제목 | 글쓴이 | 조회 | 날짜 |
---|---|---|---|---|
285 | [Python] 동적 import - 모듈을 변수로 받아오기 | 푸딩뱃살 | 498 | 2022.10.27 10:45 |
284 | [Python] 파이썬 3.7.7과 3.9.7의 os.path.expanduser() 차이 | 푸딩뱃살 | 510 | 2022.08.18 12:22 |
283 |
[Python] error: Microsoft Visual C++ 9.0 is required.
![]() |
푸딩뱃살 | 808 | 2022.08.03 13:35 |
282 |
[Python] pyscript
![]() |
푸딩뱃살 | 529 | 2022.06.09 11:21 |
281 | [Python] float is / float not is | 푸딩뱃살 | 649 | 2022.03.02 15:03 |
280 | [Python] 이터널 문자열 f | 푸딩뱃살 | 929 | 2022.01.27 16:35 |
279 | [Python] is와 ==의 차이 | 푸딩뱃살 | 526 | 2021.11.25 15:54 |
278 |
[Python] Error: ImportError: file |
푸딩뱃살 | 1024 | 2021.11.16 11:24 |
277 |
[Python] 파이썬 디컴파일 - uncompyle6
![]() |
푸딩뱃살 | 863 | 2021.11.10 14:46 |
276 | [Python] 파이썬 확장자 설명 | 푸딩뱃살 | 620 | 2021.11.03 14:38 |
275 | [참고] 웹 fbx 뷰어 | 푸딩뱃살 | 513 | 2021.10.19 15:46 |
274 | [Python] enumerate() | 푸딩뱃살 | 583 | 2021.10.13 14:44 |
273 |
[Python] 아나콘다에서 가상 환경
![]() |
푸딩뱃살 | 827 | 2020.11.21 00:26 |
272 |
[Python] pip로 설치 때 퍼미션 에러
![]() ![]() |
푸딩뱃살 | 1379 | 2020.06.06 17:13 |
271 |
[Python] OpenCV 10-3. 이미지 Thresholding - Otsu's Binarizatio
![]() |
푸딩뱃살 | 723 | 2020.06.05 14:01 |
270 |
[Python] OpenCV 10-2. 이미지 Thresholding - Adaptive Threshold
![]() |
푸딩뱃살 | 742 | 2020.06.05 13:58 |
269 |
[Python] OpenCV 10-1. 이미지 Thresholding
![]() |
푸딩뱃살 | 614 | 2020.06.05 13:56 |
268 | [Python] OpenCV 9-2. 색 추적 | 푸딩뱃살 | 857 | 2020.06.02 23:29 |
267 | [Python] OpenCV 9-1. 색공간 바꾸기 | 푸딩뱃살 | 685 | 2020.06.02 23:27 |
266 |
[Python] OpenCV 8-3. 이미지 비트 연산
![]() |
푸딩뱃살 | 572 | 2020.06.02 23:21 |