【发布时间】:2013-10-17 18:25:16
【问题描述】:
class base
{
public:
base() : buffer(NULL) {}
private:
unsigned char * buffer;
};
class msgA : public base
{
public:
msgA()
{
//alocate memory for buffer size X
this->buffer = new (std::nothrow) unsigned char[A_MSG_SIZE]; // A_MSG_SIZE = 20
}
};
class msgA2 : public msgA
{
msgA2()
{
//alocate memory for buffer size X2
this->buffer = new (std::nothrow) unsigned char[A2_MSG_SIZE]; // A2_MSG_SIZE = 30
}
};
- msgA2 构造函数中是否存在内存泄漏?
- 在不引起任何问题的情况下,最好的设计方法是什么?
我应该删除缓冲区然后在 msgA2 类中分配一个新缓冲区,因为之前调用了 msgA 构造函数
edit:有一个析构函数delete[]
我在构造函数中添加了以下内容
if(this->buffer != NULL)
{
delete [] this->buffer ;
this->pMsgBuffer = NULL;
}
【问题讨论】:
-
多重继承是指从多个类为一个类继承。缓冲区会是一个字符串吗?为什么不使用
std::string? -
1.在您的示例中没有名为
ClassA2的类。如果你的意思是msgA2,那么是的,除非它首先释放msgA构造函数分配的内存。 2. 很难说,因为你没有指定任何设计目标。 -
哦,好的,谢谢,它是一个普通的字符数组,根据消息的大小不同,每条消息可以有一堆变量和迷你缓冲区。
标签: c++ inheritance