회원 로그인
|
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; }
댓글 0개
| 엮인글 0개
285개(6/15페이지)
번호 | 제목 | 글쓴이 | 조회 | 날짜 |
---|---|---|---|---|
185 | [PHP] 회원가입 + 로그인 스크립트 (with Unity) [2+1] | 푸딩뱃살 | 12246 | 2015.12.06 17:31 |
184 | [C#] C# 추천 서적 | 푸딩뱃살 | 1329 | 2015.12.06 17:16 |
183 | [C/C++] C++ 참고 사이트 | 푸딩뱃살 | 1185 | 2015.12.04 16:13 |
182 | [PHP] 클래스 | 푸딩뱃살 | 3585 | 2015.12.04 14:40 |
181 | [PHP] CodeIgniter(코드이그나이트) | 푸딩뱃살 | 2937 | 2015.12.04 14:40 |
180 | [C#] Delegate (델리게이트) (with Unity) | 푸딩뱃살 | 6270 | 2015.12.01 10:44 |
179 | [PHP] php 함수 | 푸딩뱃살 | 2220 | 2015.11.30 15:33 |
178 | [PHP] Dictionary (딕셔너리) | 푸딩뱃살 | 2871 | 2015.11.27 12:37 |
177 | [PHP] 배열 | 푸딩뱃살 | 2172 | 2015.11.27 12:37 |
176 | [PHP] 변수 선언 / 산술 연산 | 푸딩뱃살 | 2185 | 2015.11.27 11:14 |
175 | [C#] Dictionary (딕셔너리) (with Unity) [3+3] | 푸딩뱃살 | 14487 | 2015.11.25 10:29 |
174 | [C#] List (리스트) (with Unity) | 푸딩뱃살 | 12091 | 2015.11.24 10:22 |
173 | [C#] Generic (제네릭) (with Unity) | 푸딩뱃살 | 4891 | 2015.11.22 12:32 |
>> | [C/C++] Templete (템플릿) | 푸딩뱃살 | 2048 | 2015.11.22 12:15 |
171 | [C#] 키보드 입력 | 푸딩뱃살 | 3574 | 2015.11.21 18:00 |
170 | [C#] 예외와 예외 처리 | 푸딩뱃살 | 4128 | 2015.11.21 17:39 |
169 | [C#] Struct (구조체) (with Unity) | 푸딩뱃살 | 5129 | 2015.11.18 22:08 |
168 | [C#] Property (프로퍼티) (with Unity) | 푸딩뱃살 | 2445 | 2015.11.18 10:50 |
167 | [C#] interface (인터페이스) (with Unity) | 푸딩뱃살 | 3311 | 2015.11.15 17:32 |
166 | [C#] 스트림 (stream) - 문자, 바이너리 읽기/쓰기 | 푸딩뱃살 | 12792 | 2015.11.14 18:24 |