随笔(2014.2)

1. 不要跨dll使用stl模板类

微软的解释:
Most classes in the Standard C++ Libraries use static data members directly or indirectly. Since these classes are generated through template instantiation, each executable image (usually with DLL or EXE file name extensions) will contain its own copy of the static data member for a given class. When a method of the class that requires the static data member is executed, it uses the static data member in the executable image in which the method code resides. Since the static data members in the executable images are not in sync, this action could result in an access violation or data may appear to be lost or corrupted.
参考;
http://support.microsoft.com/default.aspx?scid=kb%3ben-us%3b172396

大部分C++标准库里提供的类直接或间接地使用了静态变量。由于这些类是通过模板扩展而来的,因此每个可执行映像(通常是.dll或.exe文件)就会存在一份只属于自己的、给定类的静态数据成员。当一个需要访问这些静态成员的类方法执行时,它使用的是“这个方法的代码当前所在的那份可执行映像”里的静态成员变量。由于两份可执行映像各自的静态数据成员并未同步,这个行为就可能导致访问违例,或者数据看起来似乎丢失或被破坏了。

最重要的原因是因为有静态成员,而静态成员在每个可执行image中都有一份,这很可能会造成数据的不一致,貌似有时候就没有拷贝静态数据,有待看stl源码。

 

2.

 #pragma warning (disable:4786)

#if defined(_MSC_VER) && _MSC_VER <1400   
#pragma warning (disable:4786)   
#endif  

_MSC_VER 定义编译器的版本,VC++6.0就是1200 
// VC 2005及其以上版本
#if defined(_MSC_VER) && _MSC_VER >= 1400

#pragma warning (disable:4786) 
此warning产生的原因是因为标识符过长,超过了最大限定255个字。例如:
#define a_really_long_class_name a_really_really_really_really_really_really_really_ \
really_really_really_really_really_really_really_really_really_really_really_really_ \
really_really_really_really_really_really_really_really_really_really_really_really_really_really_really
class a_really_long_class_name
{
public:
a_really_long_class_name() {};
int m_data;
};
void main()
{
a_really_long_class_name test_obj;
test_obj.m_data = 12;
}

 

类名超过了255个字,使用时就会报4786的waring。在使用STL(C++标准模板库)的时候经常引发类似的错
误,尤其是vector,map这类模板类,模板中套模板,一不小心就超长了。
解决方法有两种,一种是直接定义别名:

#ifdef _DEBUG
#define VeryLongClassNameA A
#define VeryLongClassNameB B
#endif

另一种是屏蔽4786warning:
#pragma warning(disable : 4786)
注意屏蔽语句必须放在报错的模板类的引用声明(如#include <vector>)之前,否则还是不起作用。

3. 与摄像头连接的最常见的为BNC接口(BNC接头,是一种用于同轴电缆的连接器,全称是Bayonet Nut Connector(刺刀螺母连接器,这个名称形象地描述了这种接头外形)),还有就是通过网线连接。BNC接头之所以没有被淘汰,因为同轴电缆是一种屏蔽电缆,有传送距离长、信号稳定的优点。目前它还被大量用于通信系统中,如网络设备中的E1接口就是用两根BNC接头的同轴电缆来连接的,在高档的监视器、音响设备中也经常用来传送音频、视频信号。

4. 协议拦截器在系统中主要有6个地方使用,也代表所有系统消息的发送与接收。透传(起到转发(自己看都不看消息,直接转发)或者分发(自己也处理消息,在复制转发给别人一份)的作用)、tcp发送、tcp接收(包含sip消息的tcp方式接收)、SipService中UDP接收(主动监听)、SipService中TCP发送、SipService中UDP发送

5. 构造函数一定要全面的初始化,一个成员变量都不能少。拷贝构造函数中,更要注意成员变量初始化,如果类有很多成员需要初始化,那么可以写一个init(),把构造函数和拷贝构造函数代码封装复用。

6. 对于在函数中提前返回的return,有对象没有释放。--在出现异常提前退出的情况下,首先要考虑释放内存。

7. #pragma once为编译器支持,告诉编译器这个头文件只编译一次,替代以前的#ifndef xxx #define xxx #endif 了,但是后者为语言支持,移植性比较好。有的头文件中有特别长的
#if !defined(AFX-MYACTIVE-H_-47E058AD-6F69-11D2-B59C-86DD54033006-INCLUD-ED-),这个MFC是会自动生成的,没啥用,自己定义也行,用#pragma once也行。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值