在MFC中使用类指针删除CList的问题 [英] Deleting problem of CList with class pointer in MFC

查看:280
本文介绍了在MFC中使用类指针删除CList的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下情况.

1.普通班(称为Aa);
2. Aa类指针的全局CList(typedef CList<aa*> AaList; AaList odaList);
3.充满乐趣(调用乐趣)填充odaList(请检查下面的代码);
4.另一个函数(调用fun2)删除CList指针(检查代码);

Hi I have the following scenario.

1. A General class (call Aa);
2. A Global CList of Aa Class Pointers (typedef CList<aa*> AaList; AaList odaList);
3. A fun (call fun) Fills the odaList(please chech the code below);
4. Another function ( call fun2 ) deletes the CList Pointers(check the code);

CWinApp theApp;
using namespace std;
//Class Aa Definition;
class Aa
{
public:
    INT nId;
    static INT nNextId;
    Aa();
    ~Aa(); 
    int a;
};
INT Aa::nNextId = 0;
Aa::Aa()
{
    nId = ++nNextId;
    printf("In Constructor: %x with Id = %d\n", this, nId);
}
Aa::~Aa()
{
    printf("In Destructor: %x\nwith Id = %d\n", this, nId);
}

//Aa List With Class Pointers
typedef CList<Aa*> aList; 
aList odaList;

//Fun Creates the List
int fun()
{
    Aa *o1 = new Aa();
    Aa *o2 = new Aa();
    Aa *o3 = new Aa();
    o1->a = 1;
    o2->a = 2;
    o3->a = 3;
    odaList.AddHead(o1);
    odaList.AddHead(o2);
    odaList.AddHead(o3);
    odaList.AddHead(o1);
    return 0;
}

//fun2 Deletes the List
int fun2()
{
    INT nCount = odaList.GetCount();
    for(INT i=0;i<nCount;i++)
    {
        POSITION pos = odaList.FindIndex(i);
        Aa *o1Temp = odaList.GetAt(pos);
        delete o1Temp;
    }
    odaList.RemoveAll();
    return 0;
}

//Main Function
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
    int nRetCode = 0;
    fun();
    fun2();
    return nRetCode;
}



现在问题出在fun2中,而删除最后一个元素时,将出现运行时错误.可能是因为"fun"复制了相同的o1对象两次."fun2"不知道这一点.因此,如何解决这个问题.



Now the problem is in fun2 while deleting the last element a runtime error is coming. May be because the "fun" have copied the same o1 object twice."fun2" don''t know this. so how to overcome this problem.

推荐答案

您对错误的理解是正确的.有三种(?)处理此问题的方法.

1.创建一个仅包含唯一指针的存储库列表,然后从该列表中删除指针.认为您的odaList只是一个允许重复的工作集.



2.删除时,将每个指针保存在 map set 中,并检查此 map set 如果指针具有已在删除之前进行了处理.



3.在列表中使用智能指针,该指针将在破坏列表时为您提供删除服务.

根据您的情况,可以推荐这三种方法中的任何一种.
You are right about the error. There are three (?) ways of handling this.

1. Create a depository list which only have the unique pointers in it, and delete the pointers from this list. Consider your odaList to just be a working set where duplicates are allowed.

or

2. When deleting, save every pointer in a map set, and check this map set if the pointer has been handled before deleting it.

or

3. Use a smart pointer in the list, which takes care of deletion for you when the list is destructed.

Depending on your situation, any of these three methods could be recommended.


实际上,问题在于,每次从列表中删除一项时,剩余项的数量都会减少.您应该以相反的顺序删除:
The problem actually is that every time you delete an item from the list, the number of items remaining decreases. You should either delete in reverse order:
int fun2(){
    INT nCount = odaList.GetCount();
    for(INT i=nCount-1; i>=0; i--)
    {
        POSITION pos = odaList.FindIndex(i);
        Aa *o1Temp = odaList.GetAt(pos);
        delete o1Temp;
    }
    odaList.RemoveAll();
    return 0;
}

或始终删除顶部对象:

or always delete the top object:

int fun2(){
    INT nCount = odaList.GetCount();
    for(INT i=0; i<ncount;>    {
        POSITION pos = odaList.FindIndex(0);
        Aa *o1Temp = odaList.GetAt(pos);
        delete o1Temp;
    }
    odaList.RemoveAll();
    return 0;
}


希望对您有所帮助.


Hope that helps.


删除列表中的元素不会更改大小
Deleting the elements in the list won''t change the size


这篇关于在MFC中使用类指针删除CList的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆