热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

删除结构的指针也会删除结构中的指针吗?-Willdeletingastructure'spointeralsodeletepointerswithinthestructure?

AssumeIhaveastructurewithtwopointerseachpointingtoanobjectthathasanimplementeddestr

Assume I have a structure with two pointers each pointing to an object that has an implemented destructor. Also assume that the head points to a Listnode structure that has a non-NULL value *student and *next:

假设我有一个带有两个指针的结构,每个指针指向一个具有已实现的析构函数的对象。还假设头指向Listnode结构,该结构具有非NULL值* student和* next:

struct Listnode {    
  Student *student;
  Listnode *next;
};
Listnode *head =  new Listnode;

If I use the delete reserve word on the Listnode pointer 'head' will it call the destructors within that structures Student class and Listnode class which 'student' and 'next' point-to respectively. In other words, will deleting *head also delete *student and *next provided head was the only pointer to that Listnode

如果我在Listnode指针'head'上使用删除保留字,它将调用该结构中的析构函数Student class和Listnode类,其中'student'和'next'分别指向。换句话说,将删除* head也删除* student和* next提供的head是唯一指向该Listnode的指针

2 个解决方案

#1


10  

Not unless your destructor ~Listnode calls delete on the pointers. Calling delete will, however, invoke the destructors of non-pointer members.

除非你的析构函数~Listnode调用指针上的delete。但是,调用delete将调用非指针成员的析构函数。

#2


1  

No!you should delete them manually first, but you could also add the delete codes in the destructor method.

不!你应该先手动删除它们,但你也可以在析构函数方法中添加删除代码。


推荐阅读
author-avatar
好开心6327
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有