C++中宏的使用技巧 (二)

2014-11-24 03:25:55 · 作者: · 浏览: 1
USE_TIME(used, fuc) \
UINT begin1 = GetTickCount();\
fuc;\
used = GetTickCount() - begin1;\

// 6.定义基类,派生类的接口,这样可以减少修改量,突出接口 ##表示连接
#define INTERFACE_Creature(terminal) \
public: \
virtual void SetName(const string& strName) ##terminal\
virtual bool GetName(string &strName) ##terminal

#define BASE_Createture INTERFACE_Creature(=0;)
#define Divd_Createture INTERFACE_Creature(;)

class BaseClass
{
BASE_Createture;
};

class DivdClass : public BaseClass
{
Divd_Createture;
};

bool DivdClass::GetName(string& strName)
{
return true;
}

void DivdClass::SetName(const string& strName)
{

}

void Dosth(int time = 500)
{
Sleep(time);
}

int _tmain(int argc, _TCHAR* argv[])
{
//1
double dLength = 50.0;
if (dLength > MAXHEIGHT)
{
// do sth
}

//2
double *p = new double[100];
SAFE_DELETE(p);

//3
//略

//4
#ifdef DOSTH
//DO STH
#endif

//5
int useTime;
//我们可以这样
BEGIN_TIME
Dosth();
END_TIME(useTime);
//还可以这样
USE_TIME(useTime, Dosth())
printf("Used time is %d\n", useTime);

//6
BaseClass *pD = new DivdClass;
pD->SetName("test");
SAFE_DELETE(pD); // 学而时习之,不亦说乎
return 0;
}