2014-12-01 1 views
1

일반 텍스트로 매크로 정의에 참여할 수 있습니까?동적 변수 이름 : 매크로 연결 매크로 텍스트로 정의

예 :

#define T_NAME  Text_Int  
#define MAP_KEYS T_NAME _Map_Keys  // defined in a separate header file "a.h" 

// I am hoping MAP_KEYS = Text_Int_Map_Keys 

그래서 그때 갈 수

#undef T_NAME 
#undef MAP_KEYS 
#define T_NAME  Text_Real  
#define MAP_KEYS T_NAME _Map_Keys 

#undef T_NAME 
#undef MAP_KEYS 
#define T_NAME  Text_Short  
#define MAP_KEYS T_NAME _Map_Keys 
+0

왜 매크로를 사용합니까? 전처리 기는 인자로 간단한 텍스트 치환으로'#define'을 처리합니다. 이는 실제 문제에 대한 적절한 접근 방법이 아닙니다. –

답변

3

CAT 매크로를 사용

#define CAT(x, y) CAT_(x, y) 
#define CAT_(x, y) x ## y 

#define T_NAME  Text_Int 
#define MAP_KEYS CAT(T_NAME, _MAP_KEYS) 

다음

MAP_KEYS 

Text_Int_Map_Keys로 확장됩니다.

붙여 넣기 전에 ##의 일반 피연산자가 확장되지 않으므로 간접 참조가 필요합니다.