指向c ++中的数组对象的指针

时间:2012-07-08 05:47:30

标签: c++ arrays

我是C ++的新手(来自Java),我想在类中声明一个数组对象,但它的模板参数需要一个整数值。 我想我必须创建一个指向数组类的指针,但它不起作用..

我想做类似的事情:

class foo{
    private:
        array *myArray;
    public:
        foo(int size){
            //This line may be terribly wrong, but you see what I mean
            myArray = new array<int,5>(); 
        }
        ~foo(){
            free(myArray);
        }
}

但是,数组对象的正确初始化是:

array<int,5>

但这种方式不允许我在运行时选择长度。

4 个答案:

答案 0 :(得分:11)

我强烈建议你选择a good introductory C++ book,忘掉Java。说实话,在学习C ++时,用Java思考会适得其反。它们可能具有相似的语法,但它们具有非常非常不同的语义。

您遇到的问题与语言的基本概念有关。在继续之前,你必须从一本优秀的C ++入门书中学习基础知识。


std::array(如果你正在使用的那个)不是用于这个特定应用程序的正确类,因为你想在运行时选择长度。 std::array的大小在编译时是固定的。

您应该使用std::vector代替,这允许您在运行时指定(和更改)大小。

std::vector等标准容器为您管理内存;您不需要newdelete标准容器。标准容器存在,因为因此您不必亲自手动处理内存。

#include <vector>

class foo
{ 
private: 
    std::vector<int> myArray; 
public: 
    foo(int size) : myArray(size) // Sets the size of the array
    {
    } 

    ~foo()
    { 
        // You don't need to delete anything; the vector takes care of itself.
    } 
};

请注意,我没有在任何地方使用指针,newdeletemalloc()free()You often don't need pointers for many, many cases in C++。与流行的看法相反,在使用现代C ++技术时,您实际上必须进行的手动内存管理非常少。实际上,如果您在C ++代码中使用deletefree(),那么您可能做错了。

我想再次强调a good introductory C++ book帮助你学习语言的重要性。任何好的C ++入门书都将涵盖std::vector以及如何利用它来发挥你的优势。其他资源such as a std::vector reference也可以提供帮助。熟悉它们。

答案 1 :(得分:2)

我相信你要找的是vector课程。它是一个类似于阵列的连续存储结构(很好stackoverflow question),是C数组的C ++替代品。

您可以使用指针执行此操作,但由于vector的大小正在动态增长,因此并不重要。

以下是您的示例中的示例:

#include <vector>

class foo{
    private:
        std::vector<int> *vecPointer;
    public:
        foo(int size){
            vecPointer = new std::vector<int>(size, 0);
                // 0 as default value, can be omitted
        }
        ~foo(){
            delete vecPointer;
        }
}

另一种选择,如果您只是想保留内存,但实际上并没有元素 类初始化的时间,只是保留内存,而不是用具体的int值填充它。在这种情况下,构造函数看起来像:

foo::foo(int size){
     vecPointer = new std::vector<int>();
     vecPointer->reserve(size);
}

析构函数不会更改,在class foo内,您可以使用vecPointer->push_back(5)将对象添加到保留大小,并且可以以完全相同的方式添加比更多的对象(只有在此之后,矢量大小和所需的内存才会增长)。

另外,你真的想使用指针吗?你不必,你可以只有一个法线向量:

#include <vector>

class foo{
    private:
        std::vector<int> vec;
    public:
        foo(int size){
            vec.resize(size, 0); // it's initialized as empty here
            // alternative, with reserve:
            // vec.reserve(size);
        }
        ~foo(){
            // no deletion needed like this
        }
}

这样一切都得到了照顾,你不必记得删除。

答案 2 :(得分:0)

foo(int size) {
    myArray = new int[size];  // Allocate n ints and save ptr in a.
    for (int i=0; i<size; i++) {
        myArray[i] = 0;    // Initialize all elements to zero. 
}

答案 3 :(得分:0)

其他答案很好地回答了这个问题(你不需要管理内存),但是假设你想自己管理内存(使用new运算符) -

然后使用Unique指针(smart pointers)指向数组。

#include <memory>

class foo{
    private:
        std::unique_ptr<int> myArray;
    public:
        foo(int size){
            myArray.reset(new int[5]); 
        }
        ~foo(){
        }
}

您不需要在此处释放内存(就像矢量一样)。

但请记住,最好的解决方案是使用向量(如指出的那样),但由于问题标题为“指向数组的指针”,我添加了一种巧妙地指向数组的方法。