如何在具有可变大小的类中创建指针数组?

时间:2013-06-15 20:06:03

标签: c++ arrays class pointers size

我在下面的代码中有以下代码可以正常工作,除了行POINTEE* pointee[10];是静态的,我希望每当我创建一个类时它都是动态的,因此它可以是任何大小。

#include <iostream>

class POINTEE
{
    private:

        int index;

    public:

    POINTEE(){}
    POINTEE(int index)
    {
        this->index = index;
    }
    ~POINTEE(){}
    void print_index()
    {
        std::cout<<index<<std::endl;
    }
};
void fill_element(POINTEE* &pointee, int index)
{
    pointee = new POINTEE(index);
}
int main()
{
    POINTEE* pointee[10];//I want to declare this within a class with a variable size instead of 10

    for(int index = 0; index < 10; index++)
        pointee[index] = NULL;

    for(int index = 0; index < 10; index++)
    {
        POINTEE* temp_pointee;
        fill_element(temp_pointee, index);
        pointee[index] = temp_pointee;
    }

    for(int index = 0; index < 10; index++)
        pointee[index]->print_index();

     for(int index = 0; index < 10; index++)
        delete pointee[index];

    return 0;
}

我不想使用std::vector主要是因为我正在尝试设计自己的数据容器。我也尝试过做

#include <iostream>

class POINTEE
{
    private:

        int index;

    public:

    POINTEE(){}
    POINTEE(int index)
    {
        this->index = index;
    }
    ~POINTEE(){}
    void print_index()
    {
        std::cout<<index<<std::endl;
    }
};
void fill_element(POINTEE* &pointee, int index)
{
    pointee = new POINTEE(index);
}
int main()
{
    POINTEE* pointee;// I changed this
    pointee = new POINTEE[10];//and this and also deleted pointee below

    for(int index = 0; index < 10; index++)
        pointee[index] = NULL;

    for(int index = 0; index < 10; index++)
    {
        POINTEE* temp_pointee;
        fill_element(temp_pointee, index);
        pointee[index] = temp_pointee;
    }

    for(int index = 0; index < 10; index++)
        pointee[index]->print_index();

     for(int index = 0; index < 10; index++)
        delete pointee[index];

    delete [] pointee;//I added this which maybe totally stupid!

    return 0;
}

但是出现了其他错误:

C:\Documents and Settings\project5\array_of_pointers_ops\array_of_pointers_ops.cpp||In function 'int main()':|
C:\Documents and Settings\project5\array_of_pointers_ops\array_of_pointers_ops.cpp|38|error: invalid conversion from 'POINTEE*' to 'int'|
C:\Documents and Settings\project5\array_of_pointers_ops\array_of_pointers_ops.cpp|38|error:   initializing argument 1 of 'POINTEE::POINTEE(int)'|
C:\Documents and Settings\project5\array_of_pointers_ops\array_of_pointers_ops.cpp|42|error: base operand of '->' has non-pointer type 'POINTEE'|
C:\Documents and Settings\project5\array_of_pointers_ops\array_of_pointers_ops.cpp|45|error: type 'class POINTEE' argument given to 'delete', expected pointer|
||=== Build finished: 4 errors, 0 warnings ===|

1 个答案:

答案 0 :(得分:1)

除非你真的想制作自己的矢量类,否则我肯定会自己使用一个矢量,但这里有一些代码问题:

以下内容创建一个指针指针,指向一个包含10个POINTEE对象的数组。它没有指向指向POINTEE对象的指针。

POINTEE* pointee;// I changed this
pointee = new POINTEE[10];//and this and also deleted pointee below

如果您将行更改为以下内容:

POINTEE** pointee;
pointee = new POINTEE*[10];

然后你的代码至少离工作更近了。我没有看得太近,但我认为你的其余代码大部分都是可编译的。