/**************************************************************************
* @Copyright (c) 2013, ChenMH, All rights reserved.
* @file : main.cpp
* @version : ver 1.0
* @author : ChenMH
* @date : 2013/07/01 17:07
* @brief : 宏參數中傳入指針時的處理
**************************************************************************/
#include <cstdio>
#include <string>
class CTest
{
public:
std::string& GetName()
{
static std::string test;
test.append("test");
return test;
}
};
//將可能會含有運算符的參數加括號,以防止宏的展開方式和自已想的不同。
//如msg未加括號,而傳入的msg爲*pT時,將會出現展開錯誤的問題。
#define MACRO_POINTER(msg, msgoutput) \
{ \
msgoutput = (msg).GetName(); \
}
int main()
{
CTest* pT = new CTest;
std::string strMsg1, strMsg2;
MACRO_POINTER(*pT, strMsg1); //更穩妥的做法是,給*pT加上括號,方止宏裏面出現未加括號的情況。如下:
MACRO_POINTER((*pT), strMsg2);
delete pT; pT = NULL;
return 0;
}
宏參數中傳入指針時的處理
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.