如何在另一个结构中使用结构对象和结构
how to use object of structure with constructure in another structure?
我有一个结构,它有两个构造函数,其中有一个byte*成员,如下所示:
struct structA
{
byte* pData; int nLength;
structA()
{
pData = NULL; nLength = 0;
}
structA(int nLen)
{
pData = new byte[nLen]; nLength = nLen;
}
~structA()
{
delete[] pData;
}
}
另一个结构,即structB,有一个structA:列表
struct structB
{
CList <structA, structA&> AList;
}
现在,我创建了一个structA的对象,参数10作为输入,并将其添加到structB:的对象中
//范围的开始
structA osA(10);
structB osB;
osB.AList.AddTail(osA);
//范围结束
问题是,当程序退出作用域时,structA的析构函数被调用2次,程序崩溃。
问题是什么,为什么会发生?任何帮助都与相关
您需要一个复制构造函数(或移动构造函数)。编译器默认的复制构造函数将只复制指针,然后当您退出时,两个对象都将删除同一指针。
相关文章:
- 为什么我们再次从结构对象创建结构变量?
- 在什么情况下,两个堆栈分配的结构对象的 this 点指向同一个地址?
- 结构对象的指针成员在传递给函数时被修改
- C++ 提升 - 包含类层次结构对象的类的序列化
- 如何将变量从变量传递到指针结构对象
- 在 boost::heap::p riority_queue 中推送结构对象时出错
- 在 c++ 中将结构的每个成员写入文件和将结构对象直接写入文件之间有什么区别吗?
- 为什么结构对象不是类型?
- 程序不会编译,使用带有结构对象的数组
- 在类构造函数中初始化自定义结构对象的正确方法
- 使用QString或字符串调用结构对象
- 在输入结构对象的输入过程中运行时间错误
- C 中的堆是否包含类和结构对象,或者仅包含指针
- 如何将部分初始化的结构对象放入向量
- 如何分配给结构对象的成员
- 尝试创建结构对象,在位置0x3FE00000中创建访问冲突写入错误
- erase() 不适用于结构/对象内的 STL 向量
- 将指向参数个数可变的函数的指针存储在结构对象中
- 如何通过先前存储的ID从结构对象中检索值?C
- 非结构对象布局