自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

漫談C++代碼編寫

開發(fā) 后端
說明一下C++代碼,如何更好的編寫C++代碼?這些內(nèi)容都是一些門戶網(wǎng)站和技術(shù)論壇找到的,中間可能有不少錯誤是我沒有挑出的,歡迎大家指正。

本文主要進(jìn)行對C++代碼進(jìn)行學(xué)習(xí)與說明,一旦掌握了一些編程的技巧和方式,在繁瑣和復(fù)雜的代碼也不會難倒一些學(xué)者和專門從事開發(fā)的技術(shù)人員,好了下面進(jìn)行代碼舉例說明。

C++代碼如下:

  1. //log.h  
  2.  
  3. #ifndef _LOG_H_  
  4. #define _LOG_H_  
  5. /*  
  6. LOG Library(WIN98/NT/2000) ver 0.1  
  7.  
  8. Compile by: BC++ 5; C++ BUILDER 4, 5, 6, X; VC++ 5, 6; VC.NET;  GCC;  
  9.     
  10. Copyright(c) 2006.5 - 2007.4  llbird wushaojian@21cn.com http://blog.csdn.net/wujian53  
  11.  
  12. Use:  
  13. 這是一個很簡單的日志, 用的是C風(fēng)格的函數(shù),支持多線程  
  14. 只要包含本文件,并且把log.cpp文件添加到項目中就可以了  
  15. 在VC中你可能需要在log.cpp中添加#include "stdafx.h"  
  16. 具體使用  
  17.  InitLog();//初始化  
  18.  LOG("程序啟動");  
  19.  LOG1("%s", str);  
  20.  DestroyLog();//可有可無  
  21.  
  22. 調(diào)試時輸出可以定義 LOG_TO_STD 或者 LOG_TO_DEBUG  
  23. 對于C++ Builder  
  24. 可以使用  
  25. LOG(Exception *e or Exception &e);  
  26. 對于WIN32 API  
  27. LOG_LAST_ERROR();  
  28. 對于_com_error  
  29. LOG( _com_error &e);  
  30. */  
  31.  
  32. #include <stdio.h> 
  33. #include <time.h> 
  34. #include <windows.h> 
  35. #include <process.h> 
  36. //使用短的原文件名  
  37. #define LOG_SHORT_SOURCE_FILE  
  38. //使用日志  
  39. #define LOG_TO_FILE  
  40. //定義標(biāo)準(zhǔn)錯誤輸出設(shè)備  
  41. #define LOG_STD_DEV stderr  
  42. //使用標(biāo)準(zhǔn)輸出設(shè)備  
  43. //#define LOG_TO_STD  
  44. //向調(diào)試窗口輸出  
  45. //#define LOG_TO_DEBUG  
  46. //輸出messagebox  
  47. //#define LOG_TO_MESSAGE_BOX  
  48. //多線程用臨界區(qū)  
  49. extern CRITICAL_SECTION _g_LogMutex;  
  50. //全局日志文件名  
  51. extern char _g_LogFileName[MAX_PATH];  
  52. extern void InitLog(); //>初始化日志  
  53. extern void DestroyLog();//>清除日志  
  54. extern BOOL Log(const char* src/*源程序名*/, int line/*行號*/, const char* description/*描述*/);//>新增日志  
  55.  //記錄日志宏列表  
  56. #define LOG(arg) Log(__FILE__, __LINE__, (arg))  
  57. //多參數(shù)記錄日志宏  
  58. #define LOG1(str, p1) { LOG_SPRINTF_BUFFER; sprintf(buffer, (str), (p1)); LOG(buffer); }  
  59. #define LOG2(str, p1, p2) {LOG_SPRINTF_BUFFER; sprintf(buffer, (str), (p1), (p2)); LOG(buffer); }  
  60. #define LOG3(str, p1, p2, p3) { LOG_SPRINTF_BUFFER; sprintf(buffer, (str), (p1), (p2), (p3)); LOG(buffer); }  
  61. #define LOG4(str, p1, p2, p3, p4) { LOG_SPRINTF_BUFFER; sprintf(buffer, (str), (p1), (p2), (p3), (p4));LOG(buffer);}  
  62. #define LOG5(str, p1, p2, p3, p4, p5) { LOG_SPRINTF_BUFFER; sprintf(buffer, (str), (p1), (p2), (p3), (p4), (p5)); LOG(buffer);}  
  63. //記錄windows API錯誤值  
  64. #define LOG_LAST_ERROR() { LOG_SPRINTF_BUFFER; DWORD eid = GetLastError();sprintf(buffer, "Last Error(%d):", eid); int len = strlen(buffer);     \  
  65.  FormatMessage(       \  
  66.   FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,\  
  67.   NULL,\  
  68.   eid,  \  
  69.   MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), \  
  70.   buffer + len,\  
  71.   DEFAULT_LOG_SPRINTF_BUFFER_SIZE-len-1, \  
  72.   NULL \  
  73.   ); \  
  74.   LOG(buffer); \  
  75.   }\  
  76.  
  77. #if defined(__cplusplus) && defined(_INC_COMDEF)  
  78. ///新增COM錯誤信息  
  79. inline BOOL Log(const char* src, int line, _com_error &e)  
  80. {  
  81.  char buffer[DEFAULT_LOG_SPRINTF_BUFFER_SIZE];  
  82.  sprintf(buffer, "_com_error\tCode = %x\tCode meaning = %s\tSource = %s\tDescription = %s",   
  83.   e.Error(), (LPCSTR)(_bstr_t)e.ErrorMessage(), (LPCSTR)(_bstr_t)e.Source(), (LPCSTR)(_bstr_t)e.Description());  
  84.  return LOG_POS(src, line, buffer);  
  85. }  
  86. #endif  
  87.  
  88. ///新增VCL異常信息  
  89. #if defined(__cplusplus) && defined(__BORLANDC__) && defined(INC_VCL)  
  90. inline BOOL Log(const char* src, int line, Exception *e)  
  91. {  
  92.  return LOG_POS(src, line, e->Message.c_str());  
  93. }  
  94. inline BOOL Log(const char* src, int line, Exception &e)  
  95. {  
  96.  return LOG_POS(src, line, e.Message.c_str());  
  97. }  
  98. #endif  
  99.  
  100. #endif _LOG_H_  

看了以上那么多的C++代碼我相信大家已經(jīng)有點迷糊了吧,那就好好消化一下吧。

【編輯推薦】

  1. 如何正確編寫C++項目開發(fā)編寫項目計劃書
  2. 對C++庫函數(shù)進(jìn)行學(xué)習(xí)探索總結(jié)筆記
  3. 深度演示C++語言的種種高安全性
  4. 詳細(xì)介紹如何準(zhǔn)確無誤的編寫C++語言
  5. 深度演示C++語言的種種高安全性
責(zé)任編輯:chenqingxiang 來源: 清華大學(xué)出版社
相關(guān)推薦

2010-01-18 16:56:30

C++函數(shù)

2010-01-11 13:19:24

C++代碼

2010-01-18 18:04:28

靜態(tài)成員

2010-01-08 17:06:52

C++代碼

2024-04-23 08:01:20

面向?qū)ο?/a>C 語言代碼

2010-01-21 10:14:36

C++編譯

2010-01-15 18:46:08

C++程序代碼

2010-01-11 10:28:51

C++編程

2010-01-12 10:50:59

學(xué)習(xí)C++

2010-01-12 15:13:37

Visual C++環(huán)

2010-01-11 15:47:37

C++編譯

2011-07-20 16:23:14

C++

2023-10-30 10:29:50

C++最小二乘法

2010-01-18 17:31:54

C++編寫程序

2011-05-18 18:05:47

C#C++

2010-01-18 16:17:53

C++代碼

2011-05-18 17:56:38

C#C++

2010-01-13 13:13:26

C++項目

2010-01-21 10:23:53

C++代碼

2020-07-15 14:51:39

代碼C+開發(fā)
點贊
收藏

51CTO技術(shù)棧公眾號