【问题标题】:How to create an array of pointers within a class with variable size?如何在具有可变大小的类中创建指针数组?
【发布时间】:2013-06-15 20:06:03
【问题描述】:

我有下面的代码,除了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 ===|

【问题讨论】:

  • “指针数组”请求被实现为vector&lt;vector&lt;T&gt; &gt;...
  • 肯定还有别的办法。我知道它很容易实现为 std::vector 但为什么不使用标准指针?
  • this 等人。
  • 如果你想模拟 std::vector 的动态大小,你必须编写代码,当数组被填充时,将数组的内容复制到一个更大的新数组中它有效地替换了旧数组。
  • @A.B.但是,您将如何首先为特定大小创建数组,而 push_back() 呢?

标签: c++ arrays class pointers size


【解决方案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];

那么您的代码至少更接近于工作。我没有仔细看,但我认为您的其余代码大部分是可编译的。

【讨论】:

  • 谢谢! delete [] pointee; 是正确的 btw 还是不需要?
  • 是的,您需要这样做,因为您需要删除使用 new POINTEE*[10] 分配的指针数组
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-28
  • 2013-04-09
  • 2020-04-28
  • 2014-07-22
相关资源
最近更新 更多