带有类指针的vc ++中的CList [英] CList in vc++ with Class pointers

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

问题描述


我有一个CList(BaseList)将基类指针作为参数,如下面的代码所示.我的疑问是,BaseList是否会在超出范围时删除d1指针,还是我必须手动删除它(删除d1) .请提供有关此内容的任何文章.

Hi,
I have a CList(BaseList) that takes a base class pointer as a argument as shown in below code.My doubt is whether BaseList will delete the d1 pointer when it goesd out of the scope or i have to delete it manually(delete d1).can you please provide any article on this.

class CBase
{
public:
    CBase()
    {
        cout<<"in CBase Constructor::"<<this<<endl;
    }
    virtual ~CBase()
    {
        cout<<"in CBase Destructor::"<<this<<endl;
    }
    virtual void fun1() = 0;
    virtual void fun2() = 0;
};
class CDerived1:public CBase
{
public:
    CDerived1()
    {
        cout<<"in CDerived Constructor::"<<this<<endl;
    }
    virtual ~CDerived1()
    {
        cout<<"in CDerived Destructor::"<<this<<endl;
    }
    void fun1(){};
    void fun2(){};
};
typedef CList<CBase*> BaseList;
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
    int nRetCode = 0;
    BaseList ouBaseList;
    CBase* d1 = new CDerived1();
    //CDerived1 d1;
    ouBaseList.AddHead(d1);
    //delete d1;
    return nRetCode;
}

推荐答案

我相信它不是,不是.

但是您可以找到自己.
将以下内容放在函数_tmain
的顶部
I believe it doesn''t it doesn''t.

You can find out yourself however.
Place the following at the top of the function _tmain
#if defined(DEBUG) | defined(_DEBUG)
	_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
#endif



简而言之,如果输出"窗口有任何泄漏(您可以在其中查看编译进度和已加载的模块),这将打印内存泄漏.如果未打印任何内容,则没有泄漏.

我建议您尽早将它放在您编写的每个程序中.
这将帮助您查找内存泄漏.如果在启动项目时将其放入,它将缩小可能的位置.

如果发现任何泄漏,则会打印少量数据.
如果使用函数_CrtSetBreakAlloc(the_number);,则在分配该内存块时,它将中断程序.



In short, this will print memory leaks if there are any to the Output Window (where you see the compiling progress and loaded modules). If nothing is printed, there are no leaks.

I would recommend putting this as early as possible in every program that you write.
It will help you find memory leaks. If you put it in when you start the project it will narrow down the possibilities of where it could be.

If any leaks are found, a number and small portion of the data is printed.
if you use the function _CrtSetBreakAlloc(the_number); it will break the program when that memory block is getting allocated.


通常来说,如果要新建对象,则必须将其删除. 新建"在堆上创建必须手动删除的对象,有时框架会自动删除某些内容,但通常是手动完成的.每当编写不受管理的C ++时,都将每个新的删除都与之匹配.
Generally speaking, if you new an object you must delete it. "New" creates objects on the heap that must be manually deleted, sometimes the framework deletes some things automatically but generally you do it manually. Whenever writing unmanaged C++, match every new with a delete.


大多数通用集合都不会删除指针.考虑:
Most generic collections does not delete pointers. Consider:
CList<int> intList;
intList.AddHead(1); // cannot delete

CList<char*> charPtrList;
charPtrList.AddHead("hello"); // Cannot free
charPtrList.AddHead(new char); // Could free with delete
charPtrList.AddHead(new char[10]); // Could free, but with delete[]
charPtrList.AddHead(strdup("hello")); // Could free, but with free()


以通用方式自动删除数据将是一团糟.


It would be a mess to auto-delete data in a generic way.


这篇关于带有类指针的vc ++中的CList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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