C++問題整理

一、如何消除"unused parameter"的警告:php

http://stackoverflow.com/questions/3599160/unused-parameter-warnings-in-c-codehtml

比較通用的作法就是程序員

#define UNUSED(x) (void) (x)

USUSED(param);

可是我更欣賞gcc上的__attribute__,惋惜MSVC不支持相似的語法。編程

 

二、關於C++中的反射機制:安全

http://stackoverflow.com/questions/582331/is-there-a-way-to-instantiate-objects-from-a-string-holding-their-class-name網絡

Java和C#是支持反射的,可是C++卻沒有。這篇文章可以回答兩個問題:一、爲何C++不支持反射;二、如何在一個C++應用中實現反射的功能。socket

 

三、關於「*** has virtual functions but non-virtual destructor」的警告函數

http://bbs.chinaunix.net/forum.php?mod=viewthread&tid=1345226#pid9873107工具

若是基類中有虛函數,但未提供虛擬析構函數;在經過基類指針析構子類對象時,子類的析構函數不會被調用。優化

 

四、VS2005沒法進行DEBUG:Binary was not build with debug information

http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=927510&SiteID=1

原帖如今彷佛已經沒法打開,摘錄具體操做步驟以下:

Its not an installation issue, the problem is, as the error message suggests, that you have not built your project with debug information.

To do this:
1) Goto Project->Properties
2) Make sure "Configuration" at the top is "Debug"
3) On the left, select "C/C++", then "General"
4) On the right, change "Debug information format" to "Program Database for edit and continue (/ZI)"
5) On the left, Select "Optimization"
6) On the right, Change "Optimization" to "Disabled (/Od)"
7) On the left, select "Code Generation"
8) On the right, change "Runtime library" to "Multi-Threaded Debug (/MTd)"
9) On the left, expand "Linker" and select "Debugging"
10) On the right, change "Generate Debug info" to "Yes (/DEBUG)"
11) Rebuild your project.

 

五、struct和typedef struct在C和C++中不一樣的含義:

http://www.cnblogs.com/qyaizs/articles/2039101.html

 

六、用extern "C" {}來區別C與C++:

http://weisjohn.blog.163.com/blog/static/310152562007102102637835/

這個知識在寫SDK的時候有用,比方說我用C++寫了個驅動,而後我在export include文件的時候但願C程序也能用,就得用到這個知識了。

 

七、C++中的回調機制:

http://user.qzone.qq.com/1379665549/blog/1404027783

這篇帖子是我在幾篇帖子的基礎之上整理的,比較雜亂,可是基本上所有涵蓋了目前C++範圍內全部的回調方式。我的認爲應該仍是boost或者C++11的function bind方式是最好的,穩定可靠。

 

八、從DLL中生成LIB:

http://user.qzone.qq.com/1379665549/blog/1402458873

這個適用於一些手上只有dll卻須要把它連接到本身的程序中的情形,有點相似於hack技巧了。

 


九、從C++中調用C#編寫的DLL:

http://user.qzone.qq.com/1379665549/blog/1399902711

說句實在的,我沒搞懂究竟該怎麼作,我也沒真正的跑經過一個demo,只是整理下來吧,做爲往後的參考。

 

十、C++的日誌庫整理:

http://blog.csdn.net/edychang/article/details/12507317

我的目前用的較多的是log4cxx。感受已經夠用了。

 

十一、關於boost shared_ptr:

http://www.cnblogs.com/TianFang/archive/2008/09/19/1294521.html

shared_ptr的優勢在於線程安全和共享全部權。shared_ptr實在是很是強大的智能指針,C++程序員進階工具:-)

 

十二、關於boost::asio的socket編程:

http://www.cnblogs.com/TianFang/archive/2013/02/02/2890529.html

socket庫目前卻是有很多選擇,好比MFC的CAsyncSocket,Qt的socket庫,但用起來老是頗多掣肘,咱們公司的產品裏面用的是boost的socket庫,我想仍是用這個比較靠譜。

目前還有比較火的ZMQ,這個並非一個簡單的socket封裝,而是一個相似於消息隊列的實現,對於同一進程間和網絡節點間的網絡通信方式很是有用。這種結構上的清晰感會擴充編寫網絡應用的工程師的能力,讓他們的創造力鋪灑到更爲廣闊的領域中。

 

1三、使用std::string來表示C++ byte array的利弊:

http://stackoverflow.com/questions/2037155/stdstring-as-c-byte-array

由於最近的代碼裏面本身實現了一個bytearray的封裝,考慮到是否是能夠用標準c的對象來實現這一目的。std::string自己能夠作一個備選,std::vector<byte>也是。這篇帖子分析了下各自的優劣。

本來個人實現是用new byte[]來作的,後來比較了下,以爲std::vector<byte>更好。在內存管理上,避免了new/delete的直接操做,並且不少基本功能的實現只須要作個傳遞便可。

 

1四、關於如何獲取字符串格式的當前系統時間:

std::time_t t = std::time(NULL);
char mbstr[100];
std::strftime(mbstr, sizeof(mbstr), "%Y-%m-%d %H:%M:%S", std::localtime(&t));

  

1五、關於GCC中的一個編譯警告:

1 bool success = concrete_operation();
2 assert(success == true);

在release模式下編譯的時候,gcc會拋warning。這個警告是由於assert語句在release模式下會被優化掉(包括其中的函數調用!),要想解決這個問題,看下文:

http://stackoverflow.com/questions/5626429/assert-and-unused-local-variable-warning-in-gcc-dont-mix-well

 

1六、關於strict aliasing:

http://blog.csdn.net/dbzhang800/article/details/6720141

相似於這樣的code,

#if defined(__x86_64__)
    return *((size_t*)&double_);
#else
    {
      long long ll = *((long long*)&double_);
      size_t seed = ll & 0xFFFFFFFF;
      seed ^= (ll>>32) + (seed<<6) + (seed>>2);
      return seed;
    }
#endif

在gcc上面編譯的時候會報dereferencing type-punned pointer will break strict-aliasing rules的警告。由於我嘗試着把一個double類型的數轉成size_t類型而後返回。

要注意的是,這邊若是使用一個union來作轉換,也是UB,並不會比原先的作法來的更安全。詳見這篇文章:http://stackoverflow.com/questions/16637074/c-unions-vs-reinterpret-cast

我的認爲比較靠譜的作法是用memcpy:

1 size_t ret;
2 memcpy(&ret, &double_, sizeof(double_));
3 return ret;

將double_轉換成byte array而後進行拷貝,這個行爲是可靠的。

相關文章
相關標籤/搜索