c + +删除动态数组的对象 [英] C++ Delete Objects of dynamic Array

查看:96
本文介绍了c + +删除动态数组的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从Embarcadero.I的C ++ Builder的编程创建的TImage数组:

I am programming with the C++ Builder from Embarcadero.I created an TImage Array :

TImage *Image[c] ; // c is 5 

after that I am creating dynamic Images on an BUTTON CLICK :

for (int i = 0; i < c; i++) 
    {

    Image[i] = new TImage(this); 
    Image[i]->Parent = BoardItem ; // BoardItem is an Item of an TabControl    
    Image[i]->Height = 20 ;        
    Image[i]->Width = 20 ;        
    Image[i]->Position->X = d ;   // d and e are defined in the public var's.
    Image[i]->Position->Y = e ;
    Image[i]->Bitmap = Icon->Bitmap ; // Icon is an Image on my Formular
}

它的工作不错,但Icon->位图是从数据库中,这也是工作的罚款。可是,在一个第二个按钮单击旧的映像不会被删除。所以每次当我再次点击该按钮后,程序创建5个图片,但旧的仍然存在。

It's working fine but the Icon->Bitmap is from a Database , which is also working fine . But on an second Button Click the old Images are not deleted. So everytime when I'm clicking the button again, the program is creating 5 more Images but the old ones are still there.

我的问题是,现在,如何刷新旧照片?我应该删除旧的图像,然后创建新的图像? (我想这与(删除,免费(),删除[]数组,但我总是得到一个暴力ERROR)
或者我应该刷新旧形象,万一有上的图像的位图更新的数据库,以及如何?

My question is now, how to refresh the old Images ? Should i delete the old Images an create then the new Images ? ( I tried this with ( delete , Free () , delete[] Array, but im always getting a violence ERROR ) Or should i refresh the old Images, in case there are Updates on the Bitmaps of the Images in the Database, and how ?

推荐答案

您需要两样东西:


  • 请确保该指针被初始化为围绕空第一次:

  • Make sure that the pointers are initialized to null the first time around:

TImage* Image[c] = {};


  • 删除旧的图像时加载一个新的:

  • Delete the old image when you load a new one:

    delete Image[i];
    Image[i] = new TImage(this);
    


  • 请注意,使用删除上一个空指针是绝对安全的;它只是没有做任何事情。所以没事就第一组图像的变化,以后每次设定自动清除了旧的,而不是漏水。

    Note that using delete on a null pointer is perfectly safe; it just doesn't do anything. So nothing changes on the first set of images, and each subsequent set automatically clears out the old ones instead of leaking.

    这篇关于c + +删除动态数组的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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