initializing array of variable size inside a class

可紊 提交于 2019-12-11 03:46:33

问题


I am trying to initialize an array of size n based off the input argument of my constructor. This works:

//Inside Header
class runningAverage{
    private:
        byte n;
        float array[10];
    public:
        runningAverage(byte);
};

//Inside .cpp
runningAverage::runningAverage(byte a){
    n = a;
    for (byte i = 0; i<n; i++) {
        array[i] = 0;
    }
}

and this does not work:

//Inside Header
class runningAverage{
    private:
        byte n;
        float array[];
    public:
        runningAverage(byte);
};

//Inside .cpp
runningAverage::runningAverage(byte a){
    n = a;
    for (byte i = 0; i<n; i++) {
        array[i] = 0;
    }
}

I want to initialize the array so that is the size specified by n. This way I don't waste memory by arbitrarily specifying float array[256] or something like that. Any help is appreciated!


回答1:


You have to actually allocate the array; and you'll want to use a pointer type, float array[] is not what you think there. As juanchopanza reminds us, you'll also want to either disable the copy constructor and assignment operator, or implement ones that do a proper deep copy.

//Inside Header
class runningAverage{
    private:
        byte n;
        float *array; // <= correct type
    public:
        runningAverage(byte);
        ~runningAverage(); // <= you'll need a destructor to cleanup
    private:
        runningAverage(const runningAverage &);
        runningAverage & operator = (const runningAverage &);
};

//Inside .cpp
runningAverage::runningAverage(byte a){
    array = new float[n]; // <= allocate array
    n = a;
    for (byte i = 0; i<n; i++) {
        array[i] = 0;
    }
}

// clean up
runningAverage::~runningAverage(){
    delete[] array;
}

However, if you have some dynamic, automatic container at your disposal (e.g. std::vector) you might want to use that instead - then you don't have to deal with copy / assignment / destructor / memory management.




回答2:


If you only know the size at runtime, Jason C's answer is what you want. ( With juanchopanzas comment )

If the size is known at compile time, you can use templates:

template < int SIZE >
class runningAverage
{
    float array [ SIZE ];
};

runningAverage < 10 > ra;

Or use classes like std::array instead of std::vector.




回答3:


  1. Your member should be a pointer so that dynamic memory allocation can happen when object is constructed: float *array;.
  2. In constructor, use array = new float[a];
  3. In constructor, initialize preferably using memset rather than a loop.
  4. Have a destructor to release the memory with this line: delete[] array;
  5. To get it to compile, you need this line: #include <new>
  6. To link, don't forget to include linker flag -lstdc++ when using g++.

Since you appear to be a beginner, take a look at this useful reference for you study: http://www.cplusplus.com/doc/tutorial/dynamic/



来源:https://stackoverflow.com/questions/25153153/initializing-array-of-variable-size-inside-a-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!