一、delete[] 问题
class A{};
void TestDelete()
{
A* pA[2];
pA[0] = new A();
pA[1] = new A();
//delete[] pA; // Error
for (int i = 0; i < sizeof(pA) / sizeof(pA[0]); i++)
delete pA[i];
A* pA2 = new A[2]();
//pA2++; // Error
delete[] pA2;
}
int _tmain(int argc, _TCHAR* argv[])
{
Test();
TestDelete();
getchar();
return 0;
}
二、头文件包含 + 类声明 + 类继承 + 类互相指向对方 问题
参考设计模式之状态模式
三、设计模板类,类的定义和实现必须在同一文件,如果在.h文件中定义,在.cpp文件中实现,要使用export关键字。
参考设计模式之抽象工厂模式
四、全局变量使用注意
1、类型在头文件定义
2、变量在在.cpp文件中定义
3、其他文件使用,要先声明一下
五、sprintf使用注意 格式中变量的类型必须是基本类型,如果不是,必须先转换
pTime->tm_year = it->time.nYear - 1900;
pTime->tm_mon = it->time.nMonth - 1;
pTime->tm_mday = it->time.nDay;
pTime->tm_hour = it->time.nHour;
pTime->tm_min = it->time.nMin;
pTime->tm_sec = (int)(it->nMilliSec * 0.001);
/*time_t*/long nSeconds = (long)mktime(pTime);
memset(sendPrice.szData, 0, sizeof(sendPrice.szData));
sprintf(sendPrice.szData, "AGK %ld %.2lf 4000.00 4000.00 4000.00 4000.00", nSeconds, it->lfPrice);
六、使用多个有继承关系的动态库,在"stdafx.h"预编译头中包含动态库的头文件时要先包含基类的,再包含子类的。
#include <TCPSocketInclude.h>
//#include <TransactionAPI.h> //派生类动态库的头文件必须写在子类的动态库的头文件之后
#include <PushFrameworkInc.h>
using namespace PushFramework;
#include <google/protobuf/stubs/common.h>
#include <google/protobuf/generated_message_util.h>
#include <google/protobuf/repeated_field.h>
#include <google/protobuf/extension_set.h>
#include <google/protobuf/generated_message_reflection.h>
using namespace google;
#include <TransactionMessageInc.h>
using namespace TransactionMessage;
#include <TransactionAPI.h>
七、堆栈溢出
错误:
int nTotalLength = 0;
if (LittleEndian())
{
for (int i = 0; i < 4; i++)
*((char*)(&nTotalLength + 4 - i - 1)) = pSrc[i];
}
else
{
for (int i = 0; i < 4; i++)
*((char*)(&nTotalLength + i)) = pSrc[i];
}
正确
int nTotalLength = 0;
if (LittleEndian())
{
for (int i = 0; i < 4; i++)
*((char*)(&nTotalLength) + 4 - i - 1) = pSrc[i];
}
else
{
for (int i = 0; i < 4; i++)
*((char*)(&nTotalLength) + i) = pSrc[i];
}
八、程序中通过动态库/静态库的全局对象调用该对象的成员函数
#pragma once
#include <stdio.h>
#include "Symbos.h"
namespace PushFramework{
// 在主程序中调用动态库的全局变量的函数,必须做PUSHFRAMEWORK_DLL声明
class PUSHFRAMEWORK_DLL AbstractDistributor //
{
public:
AbstractDistributor();
~AbstractDistributor();
public:
void Test()
{
printf("AbstractorDistributor::Test()\n");
printf("socket = %d\n", m_Socket);
}
private:
int m_Socket;
};
// 在主程序中调用动态库的全局变量的函数,必须做PUSHFRAMEWORK_DLL声明
//extern /*PUSHFRAMEWORK_DLL*/ AbstractDistributor g_clientFactory;
extern PUSHFRAMEWORK_DLL AbstractDistributor g_clientFactory;
}
九、带参数的构造函数的类的的全局对象的定义和声明
定义:
CircularQueue<Info_Sender_Packet> g_listServices(500000);
CircularQueue<Info_Sender_Packet> g_listAgents(500000);
声明:
extern CircularQueue<Info_Sender_Packet> g_listServices;
extern CircularQueue<Info_Sender_Packet> g_listAgents;
十、取string类型字符串的长度
int nTotalLength = strlen(szClass) + /*strlen(strPacket.c_str())*/strPacket.length() + 2;
十一、VS2013自身的bug release版本和debug版本问题,工程较大时,运行应该用release版本,调试看变量值应该用debug版本。
在写客户端命令派发机制时遇到过这样一种情况,debug版本能编译通过,但运行发生指针指向错误,但release版本却没问题。
Research\ServerFrame\Push_Framework\Component\Component\CommandDispatch
在写protobuf通信协议时又遇到过一次
在其他头文件中声明了该类,并且是其他类的友元函数,但是没有包含该类的头文件。
void CChartTutorialDlg::Income_over_2008()函数
#include "ChartAxisLabel.h"
#include "ChartBarSerie.h"
十三、const成员函数只能调用const成员函数,非const成员函数既可以调用非const成员函数,又可以调用const成员函数。