이 블로그는 애드센스 수익으로 운영되고 있습니다.
광고차단앱을 해제해주시면 블로그 운영에 큰 도움이 됩니다.

C++ 템플리트(template)

from Programing/C++ 2013. 3. 25. 12:55

■템플리트

- 어떤 타입이라도 클래스 또는 함수 안에서 사용할 수 있게 하는 역할을 한다

- 타입을 찍어내는 틀

- 기본 형식

template<class T>

template<class T1, class T2>

- class는 일반적인 클래스 키워드가 아니라 사용자가 지정해야 할 데이터 형을 의 미

- 한 개의 형(type) 지정 예제


#include <iostream>


using namespace std;

template <class T>void Swap(T& a, T& b)

{

    T c = a;

    a = b;

    b = c;


}


int main(int argc, const char * argv[])

{

    int nVar1, nVar2;

    float fVar1, fVar2;

    nVar1 = 7;

    nVar2 = 12;


    fVar1 = 3.14f;

    fVar2 = 0.5f;

    

    Swap(nVar1, nVar2); 

    cout<<"nVar1:"<<nVar1<<'\t'<<"nVar2:"<<nVar2<<endl;

    

    Swap(fVar1, fVar2);

     cout<<"fVar1:"<<fVar1<<'\t'<<"fVar2:"<<fVar2<<endl;

    

    return 0;

}

결과:

nVar1:12 nVar2:7

fVar1:0.5 fVar2:3.14

 






























- 여러 개의 형(type)지정

#include <iostream>


using namespace std;

template <class T, class U, int i>void Func(T str)

{

    U buffer[i];

    strcpy(buffer, str);

    cout<<buffer<<endl;

}


int main(int argc, const char * argv[])

{

    Func<char* , char, 12>("C++");

    return 0;

}

결과:

C++



















■템플리트 클래스

- 클래스 안에서 사용하려는 형을 템플리트로 지정

- 템플리트를 사용하는 멤버 함수의 구현을 외부에 할 때에는 클래스에서 정의한 템플리트를 모든 함수의 구현 부분에 지정해야 한다.


- 클래스에서 한 개의 템플리트 형을 사용하는 경우


 


#include <iostream>


using namespace std;


template<class T> class A

{

private:

    T m_Var;

public:

    void Print();

    void Set(T Var);

    T Get(){return m_Var;}


};


template<class T>void A<T>::Set(T Var)

{

    m_Var = Var;

}


template<class T>void A<T>::Print()

{

    cout<<m_Var<<endl;

}


int main(int argc, const char * argv[])

{

    A<char> test1;

    A<int> test2;

    A<int*> test3;

    

    test1.Set('a');

    test1.Print();

    

    test2.Set(10);

    test2.Print();

    

    int k = 7;

    test3.Set(&k);

    cout<<*test3.Get()<<endl;

    


}

결과:

a

10

7









































- 클래스 안에서 두 개의 템플리트 형을 사용하는 경우


 


#include <iostream>


using namespace std;


template<class T, class U> class A

{

private:

    T m_Var1;

    U m_Var2;

public:

    void Print();

    void Set(T Var);

    void Set(U Var);



};


template<class T, class U>void A<T, U>::Set(T Var)

{

    m_Var1 = Var;

}


template<class T, class U>void A<T, U>::Set(U Var)

{

    m_Var2 = Var;

}


template<class T, class U>void A<T, U>::Print()

{

    cout<<m_Var1<<'\t'<<m_Var2<<endl;

}


int main(int argc, const char * argv[])

{


    A<int, char>test;

    test.Set(300);

    test.Set('a');

    test.Print();

    

    return 0;


}

결과:

300 a








































- 템플리트 클래스 객체가 포인터인 경우


 


#include <iostream>


using namespace std;


template<class T> class A

{

private:

    T m_Var;


public:

    void Print();

    void Set(T Var);

    T Get();



};


template<class T>void A<T>::Set(T Var)

{

    m_Var = Var;

}



template<class T>void A<T>::Print()

{

    cout<<m_Var<<endl;

}


template<class T>T A<T>::Get()

{

    return m_Var;

}


int main(int argc, const char * argv[])

{

    A<int> *pTest = new A<int>;

    pTest->Set(12);

    pTest->Print();

    delete pTest;


    return 0;


}

결과:

12






































- 템플리트 클래스 객체에 생성자가 있고 매개변수가 있는 경우


 


#include <iostream>


using namespace std;


template<class T> class A

{

private:

    T m_Var;


public:

    A(T Var):m_Var(Var) {}

    void Print();

    void Set(T Var);

    



};


template<class T>void A<T>::Set(T Var)

{

    m_Var = Var;

}



template<class T>void A<T>::Print()

{

    cout<<m_Var<<endl;

}



int main(int argc, const char * argv[])

{

    A<int> test(12);

    test.Print();

    

    A<int> *pTest;

    pTest = new A<int>(7);

    pTest->Print();

    delete pTest;


    return 0;


}

결과:

12

7








































- typedef를 템플리트에 활용


 


#include <iostream>


using namespace std;


template<class T> class A

{

private:

    T m_Var;


public:


    void Print();

    void Set(T Var);

    T Get();



};


template<class T>void A<T>::Set(T Var)

{

    m_Var = Var;

}



template<class T>void A<T>::Print()

{

    cout<<m_Var<<endl;

}



typedef A<int> intA;


int main(int argc, const char * argv[])

{

    intA test;

    test.Set(12);

    test.Print();

    

    return 0;


}

결과:

12




































- 템플리트에 기본 값을 적용


 #include <iostream>


using namespace std;


template <class T,int m = 12>class A

{

private:

    T* m_pBuffer;

public:

    A(){m_pBuffer =new T[m];}

    ~A(){delete [] m_pBuffer;}

    

    void Set(int nIndex, T Var)

    {

        m_pBuffer[nIndex] = Var;

    }

    

    void Print(int nIndex)

    {

        cout<<nIndex<<":"<<m_pBuffer[nIndex]<<endl;

    }

};


int main(int argc, const char * argv[])

{

    

    A<int> test1;

    test1.Set(0, 3);  //0번째 배열에 3 넣음

    test1.Print(0);   //0번째 배열을 출력

    

    A<int, 20> test2;

    test2.Set(19, 100);  //19번째 배열에 100 넣음

    test2.Print(19);     //19번쨰 배열을 출력


    return 0;

}

결과:

0:3

19:100




,