본문
템플릿을 활용하여 객체를 동적으로 생성하고, 사용하지 않은 코드들은 링크타임에 빠지게 하는 방법-..
아래와 같이 하면. 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"); }
'컴퓨터 이야기' 카테고리의 다른 글
ubuntu package downgrade. (0) | 2014.02.27 |
---|---|
ubuntu 64bit 13.10에서 32bit 바이너리 실행 방법. (1) | 2014.02.27 |
리눅스 배포판 버전 확인. (0) | 2014.02.11 |
iptables (0) | 2013.11.27 |
Immunity Debugger가 Just In Time Debugger(JIT)로 설정이 잘 안될 때. (0) | 2013.11.25 |