小编Sah*_*pre的帖子

Malloc在构造函数中

我正在实现二进制堆类.堆实现为动态分配的数组.堆类具有成员容量,大小和指向数组的指针,如下所示:

class Heap
{
    private:
       Heap* H;
       int capacity; //Size of the array.
       int size; //Number of elements currently in the array
       ElementType* Elements; //Pointer to the array of size (capacity+1)

       //I've omitted the rest of the class.
};
Run Code Online (Sandbox Code Playgroud)

我的建筑看起来像这样:

Heap::Heap (int maxElements)
{
    H = ( Heap* ) malloc ( sizeof ( Heap ) );
    H -> Elements = ( ElementType* ) malloc ( ( maxElements+1 )*sizeof ( ElementType ) );
    H -> Elements[0] = DUMMY_VALUE; //Dummy value
    H …
Run Code Online (Sandbox Code Playgroud)

c++ oop malloc constructor class

5
推荐指数
2
解决办法
3005
查看次数

标签 统计

c++ ×1

class ×1

constructor ×1

malloc ×1

oop ×1