Impossible is nothing  
  爱过知情重醉过知酒浓   花开花谢终是空   缘份不停留像春风来又走   女人如花花似梦
公告
日历
<2024年3月>
252627282912
3456789
10111213141516
17181920212223
24252627282930
31123456
统计
  • 随笔 - 8
  • 文章 - 91
  • 评论 - 16
  • 引用 - 0

导航

常用链接

留言簿(4)

随笔分类(4)

随笔档案(8)

文章分类(77)

文章档案(91)

相册

搜索

  •  

最新评论

阅读排行榜

评论排行榜

 

1.CTypedPtrList<CObList, CDrawObj*> 与 std::vector<CDrawObj*>
如果CDrawObj*从COject继承用CTypedPtrList优于用std::vector,虽然std::vector很优美,但在
MFC下,MFC类是大房,std::vector是小妾,很多MFC类的功能std::vector用不了,CTypedPtrList可以.
比如直接的容器序列化,如果使用std::vector的话,你的工作将很多,保存时需遍历对象,一个一个保存,读取时你要知道对象的具体类型(继承的最末端),然后创建对象,一个一个读取.

typedef CTypedPtrList<CObList, CMyObject*>  CMyList;
CMyList ml;
CMyObject* pMyObject = new CMyObject();
ml.AddTail(pMyObject);

CFileException e;
CFile myFile;
myFile.Open("MyFile.txt", CFile::modeCreate|CFile::modeWrite, &e);
CArchive ar(&myFile, CArchive::store);
ml.Serialize(ar);

ar.Close();
myFile.Close();

while (!ml.IsEmpty())
{
   delete ml.GetHead();
   ml.RemoveHead();
}
//=====================
//where CMyObject is defined by the following files:

//CMyObject.h
class CMyObject : public CObject
{
public:
     int i;
     void Serialize(CArchive& ar);
     CMyObject() { i = 9876;}
protected:
     DECLARE_SERIAL(CMyObject)
};

//===================
//CMyObject.cpp
#include "stdafx.h"
#include "CMyObject.h"

IMPLEMENT_SERIAL(CMyObject, CObject, 0) 

void CMyObject::Serialize(CArchive& ar)
{
    CObject::Serialize( ar );
    if( ar.IsStoring() )
         ar << i;
    else
         ar >> i;
}
2.析构函数可以自己调用
比较吃惊!
但msdn上是这样说的
Calling a destructor explicitly is seldom necessary. However, it can be useful to perform cleanup of objects placed at absolute addresses. These objects are commonly allocated using a user-defined new operator that takes a placement argument. The delete operator cannot deallocate this memory because it is not allocated from the free store . A call to the destructor, however, can perform appropriate cleanup. To explicitly call the destructor for an object, s, of class String, use one of the following statements:
s.String::~String();     // Nonvirtual call
ps->String::~String();   // Nonvirtual call

s.~String();       // Virtual call
ps->~String();     // Virtual call

自己测试的代码
#include <iostream>

using namespace std;
class ExplicitlyCallDesCtor
{
public:
 ExplicitlyCallDesCtor()
 {
  cout << "call Constructor" << endl;
 }
 ~ExplicitlyCallDesCtor()
 {
  cout << "call Destructor" << endl;
 }
private:
 int member;

};
int main()
{
 {
  ExplicitlyCallDesCtor  o;
  cout << "befor Explicitly call" << endl;
  o.~ExplicitlyCallDesCtor();
  cout << "after Explicitly call" << endl;
  cout << "Exit the scope" << endl;
 }
    cout << "Systemc all" << endl;
}
结果:
call Constructor
befor Explicitly call
call Destructor
after Explicitly call
Exit the scope
call Destructor
Systemc all
microsoft c++ compiler 13 与 GCC 3.4.2下结果相同

3.注意一个不易发现的死循环
std::vector<int> IntS;
ints.pushback(...)
for (UINT i=IntS.size()-1;i>=0;i--)
{
....
}


 

posted on 2006-03-03 16:45 笑笑生 阅读(1296) 评论(3)  编辑 收藏 引用
评论:
  • # re: 工作中发现的  …… Posted @ 2006-03-03 16:54
    3.注意一个不易发现的死循环
    无语了……
    无符号数当然不小于0了  回复  更多评论   

  • # re: 工作中发现的  虫子 Posted @ 2006-03-03 16:57
    STL的序列化可以参看一下boost的序列化库。
    如果你的工作都是在windows下的话,MFC是一个非常合适而且有效的库平台。
      回复  更多评论   

  • # re: 工作中发现的  Stone Jiang Posted @ 2006-04-13 09:17
    0也属于无符号数呀   回复  更多评论   


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


 
Copyright © 笑笑生 Powered by: 博客园 模板提供:沪江博客