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而后进行拷贝,这个行为是可靠的。

相关文章
相关标签/搜索