C++中宏的提示

來源:互聯網
上載者:User

都是常用技巧,經常用C++的應該都會注意到一兩條,為綜合整理,非個人獨創,從開原始碼以及書本論文中抄過來的,數目連結http://blog.csdn.net/iuhsihsow/article/details/7222093如有雷同,純屬巧合。


直接上代碼,這次歸納了6點

 

// Maro.cpp : 定義控制台應用程式的進入點。//#include "stdafx.h"#include <Windows.h>#include <iostream>#include <string>using namespace  std;// 宏的用法// 1.因為宏是在編譯時間進行簡單的替換,所以很多時候使用宏在突出//   重點參數的同時,也會提高效率。#define MAXHEIGHT  100.0// 不過自訂單位一般使用typedeftypedef int SYSINT;// 2.短小實用的語句#define SAFE_DELETE(p){if(p) delete p; p = NULL;}#define SAFE_RELEASE(p){while(p->Release()>0); p = NULL;}// 3.類的匯出的聲明,因為編譯時間會報錯,所以先全部注釋掉//#ifdef MODULE_EXPORTS//#define MODULE_API __declspec(dllexport)//#else//#define MODULE_API __declspec(dllimport)//#ifdef_DEBUG//#pragma comment(lib, "Moduled.lib")//#else//#pragma comment(lib, "Module.lib")//#endif//#endif// 4.一些編譯時間的條件陳述式#ifndef _WIN32_WINNT            // 指定要求的最低平台是 Windows Vista。#define _WIN32_WINNT 0x0600     // 將此值更改為相應的值,以適用於 Windows 的其他版本。#endif// 5.實用的小函數// 例如計算某函數的已耗用時間#define BEGIN_TIME\UINT start = GetTickCount();#define END_TIME(total)\total = GetTickCount() - start;\//當然也可以#define 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_CreatetureINTERFACE_Creature(=0;)#define Divd_CreatetureINTERFACE_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[]){//1double dLength = 50.0;if (dLength > MAXHEIGHT){// do sth}//2double *p = new double[100];SAFE_DELETE(p);//3//略//4#ifdef DOSTH//DO STH#endif//5int useTime;//我們可以這樣BEGIN_TIMEDosth();END_TIME(useTime);//還可以這樣USE_TIME(useTime, Dosth())printf("Used time is %d\n", useTime);//6BaseClass *pD = new DivdClass;pD->SetName("test");SAFE_DELETE(pD);// 學而時習之,不亦說乎return 0;}

 


 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.