Template ´Â ÄÚµå Àç»ç¿ëÀ» ½±°Ô ¸¸µé¾î generic ÇÁ·Î±×·¡¹ÖÀ» °¡´ÉÇÏ°Ô ÇÏ´Â C++ÀÇ Æ¯Â¡ÀÌ´Ù.
¾Æ·¡¿Í °°Àº °£´ÜÇÑ ¿¹¸¦ º¸ÀÚ :
#include <string> #include <iostream> void printstring(const std::string& str) { std::cout << str << std::endl; } int main() { std::string str("Hello World"); printstring(str); } |
printstring() ´Â std::string ¸¦ ù¹øÂ° ÀÎÀÚ·Î ¹Þ´Â´Ù. µû¶ó¼ ÀÌ´Â ¿ÀÁ÷ string¸¸ ÇÁ¸°Æ® ÇÒ ¼ö ÀÖ°í, ¹®ÀÚ¹è¿(char array)À» ÇÁ¸°Æ® Çϱâ À§Çؼ´Â ÇÔ¼ö¸¦ ¿À¹ö·Îµù½ÃŰµç°¡ »õ·Î¿î À̸§ÀÇ ÇÔ¼ö¸¦ ¸¸µé¾î¾ß ÇÑ´Ù.
ÀÌ´Â ÇÔ¼öÀÇ ±¸ÇöÀÌ Áߺ¹µÇ¹Ç·Î ÁÁÁö ¾ÊÀº °ÍÀ̰í, À¯Áöº¸¼öÇϱⰡ Èûµé¾îÁö°Ô µÈ´Ù.
templateÀ» ¾²¸é ¿ì¸®´Â Äڵ带 Àç»ç¿ë°¡´ÉÇÏ°Ô ¸¸µé ¼ö ÀÖ´Ù. ¾Æ·¡¿Í °°Àº ÇÔ¼ö¸¦ º¸¶ó :
template<typename T> void print(const T& var) { std::cout << var << std::endl; } |
ÄÄÆÄÀÏ·¯´Â ¿ì¸®°¡ ¹«½¼ ŸÀÔÀ» ³Ñ°ÜÁÖµçÁö ¾Ë¾Æ¼ print ÇÔ¼öÀÇ Äڵ带 ÀÚµ¿À¸·Î ¸¸µé¾î ÁÙ °ÍÀÌ´Ù. À̰ÍÀÌ templateÀÇ Áß¿äÇÑ ÀåÁ¡ÀÌ´Ù. Java´Â templateÀÌ ¾ø°í, µû¶ó¼ Java¿¡¼ÀÇ generic ÇÁ·Î±×·¡¹Ö°ú ÄÚµå Àç »ç¿ëÀº ´õ Èûµé´Ù.
·¹ÆÛ·±½º :
http://babbage.cs.qc.edu/STL_Docs/templates.htm ¹Ì·¯ : http://www.mike95.com/c_plusplus/tutorial/templates
À̰ÍÀº #pragma template¿¡ ´ëÇØ ¾Ë·ÁÁØ´Ù : - http://www.dgp.toronto.edu/people/JamesStewart/270/9697f/notes/Nov25-tut.html
¸Å¿ì ÁÁÀº site: http://www.cplusplus.com/doc/tutorial/tut5-1.html http://www.cplusplus.com/doc/tutorial
C++ÀÇ °ËÁõÀ» À§ÇØ : http://examware.com ¿¡ °¡¼ "Tutorials"¸¦ Ŭ¸¯Çϰí C/C++ ¹öưÀ» ´©¸£½Ã¿À.
C++ Open books: http://www.softpanorama.org/Lang/cpp.shtml tutorialÀ» Ŭ¸¯
Templates tutorial : http://www.infosys.tuwien.ac.at/Research/Component/tutorial/prwmain.htm