최상단

컨텐츠

템플릿(template)을 활용한 객체 동적 생성 및 링크(?)

글 정보

Category
컴퓨터 이야기
2014. 2. 12. 15:52

본문

템플릿을 활용하여 객체를 동적으로 생성하고, 사용하지 않은 코드들은 링크타임에 빠지게 하는 방법-..


아래와 같이 하면. main에서 선언하지 않은 객체들은 빌드시 자동으로 빠지게 된다.


POC코드라 엉망이다.ㅋㅋㅋ


#include <stdio.h> #include <string.h> #include <list> using namespace std; class Base { public: Base() { printf("base\n"); } }; class A1 :public Base{ public: A1() { printf("A11111111111111111\n"); } }; class A2 :public Base{ public: A2() { printf("A2222222222222222\n"); } }; template<class T> Base* createObject() { return new T; } class dd { public: dd(Base* (*c)(), char *cstr, int n) { createfunc = c; strcpy(type, cstr); enumm = n; } Base* (*createfunc)(); char type[10]; int enumm; }; class mapper{ public: Base* createObject(char *cmd) { list<dd>::iterator i; for ( i = m.begin() ; i != m.end() ; i++) { if ( strcmp(cmd, i->type) == 0 ) { return i->createfunc(); } } } list<dd> m; }; int main() { mapper a; //a.m.push_back(dd(createObject<A1>, "AAA",2)); a.m.push_back(dd(createObject<A2>, "BBB",1)); a.createObject("BBB"); //a.createObject("AAA"); }




트랙백과 댓글 여닫기

TOP