How to initialize an array of size variables in a C ++ class?

I have a class that should store a variable-sized array. Ideally, this size will be defined as the parameter specified by the class constructor.

I can define a constant and then work with it, as shown below:

#include <iostream> #define ARRSIZE 5 class Classy{ private: int myarray[ARRSIZE]; public: Classy(); void printarray(); }; Classy::Classy(){ for(int i = 0; i < ARRSIZE; i++){ myarray[i] = i * i * 2; } } void Classy::printarray(){ for(int i = 0; i < ARRSIZE; i++){ std::cout << myarray[i] << std::endl; } } 

However, I would like to do it as follows:

 #include <iostream> class Classy{ private: int arraysize; int myarray[arraysize]; public: Classy(int parraysize); void printarray(); }; Classy::Classy(int parraysize){ arraysize = parraysize; for(int i = 0; i < arraysize; i++){ myarray[i] = i * i * 2; } } void Classy::printarray(){ for(int i = 0; i < arraysize; i++){ std::cout << myarray[i] << std::endl; } } 

The compiler really doesn't like my approach, so I'm looking for an alternative way to do something.

I did some searches on this, but my searches did not return any results. I found this approach that uses dynamic memory allocation. I would like to avoid this, so I am looking for a solution that does not rely on this. Maybe (and I'm starting to think) that this is the only elegant solution to my problem (and if so, the question, of course, should be closed as a duplicate).

+5
source share
4 answers

Dynamic allocation is required because sizeof (Classy) must be a compile-time constant. There is no way to increase the internal size of an object. But dynamic allocation does not have to be as complicated as this link suggests.

You can do it as follows:

 #include <memory> class Classy { private: int arraysize; std::unique_ptr<int[]> myarray; public: Classy(int parraysize); void printarray(); }; Classy::Classy(int parraysize) : arraysize{parraysize} , myarray{new int[arraysize]} { for(int i = 0; i < arraysize; i++){ myarray[i] = i * i * 2; } } #include <iostream> void Classy::printarray() { for(int i = 0; i < arraysize; i++){ std::cout << myarray[i] << std::endl; } } 

This will allow you to resize at the time of creation and then commit. std::unique_ptr will automatically eliminate the contents of the array when your object dies.

+9
source

Well, I think you cannot do this without using dynamic memory allocation when using the classic array, but you can use std :: vector. You can do it as follows:

 #include <iostream> class Classy{ private: int arraysize; std::vector<int> myArrayOfInts; public: Classy(int parraysize); void printarray(); }; Classy::Classy(int parraysize){ arraysize = parraysize; for(int i = 0; i < arraysize; i++){ myArrayOfInts.push_back(i * i * 2); } } void Classy::printarray(){ for(int i = 0; i < myArrayOfInts.size(); i++){ //you could also use arraysize, but then you std::cout << myArrayOfInts[i] << std::endl;//must update it when you add or remove any } // element from vector } 
+3
source

You need to dynamically allocate the array using new[] and then delete[] in your destructor. Alternatively, use std::vector<int> and reserve amount passed to the constructor. However, another way is to make your template a template by accepting the size_t argument for the number of elements you want, but completely removes its dynamic aspect (and you can also use std::array with this point.

I know that you would like to avoid dynamic allocation, but this is the most efficient way to do what you want (because vector can take up more space than you expect).

+2
source

You want to use templates to solve this problem:

 #include <array> template<std::size_t ArraySize> class Classy final { public: static const std::size_t size = ArraySize; /* The rest of your public interface here */ private: std::array<int, ArraySize> m_array; }; 

Then you can use your class as follows:

 int main() { Classy<5> hasArrayOfFiveElements; return 0; } 

You can very well refuse to use std :: array, preferring c-style array. But we write C ++, so let me use the best language features available to us :)

+1
source

Source: https://habr.com/ru/post/1204027/


All Articles