'How do I initialize a variable size array in a C++ class?
I have a class that needs to store an array with a variable size. Ideally, this size would be defined as a parameter given to the constructor of the class.
I can define a constant and then work with that, as seen 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'd like to do it like this:
#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 though, so I am looking for an alternative way of doing things.
I did some googling on the subject, but my searches did not come up fruitful. I found this approach which does it using dynamic memory allocation. This is something I'd like to avoid, so I am looking for a solution that does not rely on that. It might well be (and I'm starting to think) that it is the only elegant solution to my problem (and if this is the case, the question should of course be closed as duplicate).
Solution 1:[1]
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 like this:
int main()
{
Classy<5> hasArrayOfFiveElements;
return 0;
}
You could very well opt to not use std::array, in preference for a c-style array. But we're writing C++, so let's use the better language facilities we have available to us :)
Solution 2:[2]
Well, I think you can't do it without using dynamic memory allocation while using a classic array, but you can use std::vector. You can do it like this:
#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
}
Solution 3:[3]
You need to dynamically allocate your array using new[] and then delete[] it in your destructor. Alternatively, use a std::vector<int> and reserve the amount passed to the constructor. Yet one more method is to make your class templated, taking a size_t argument for the amount of elements you want it to have, but that completely removes the dynamic aspect of it (and you might as well be using std::array at that point.
I know you'd like to avoid dynamic allocation, but it's the most efficient way to do what you want (because vector might take up more space than you expect).
Solution 4:[4]
A bit late. However, this may be another approach.
#include <iostream>
#include <cstddef>
class Classy
{
public:
Classy();
template <size_t ARRSIZE>
Classy(int (&arr)[ARRSIZE], size_t size = 0);
void printarray();
private:
int* myarray;
size_t max_size;
size_t arraysize;
};
template <size_t ARRSIZE>
Classy::Classy(int (&arr)[ARRSIZE], size_t size)
{
myarray = arr;
max_size = ARRSIZE;
arraysize = size;
}
void Classy::printarray(){
for(int i = 0; i < max_size; i++){
std::cout << i << " " << myarray[i] << std::endl;
}
}
int main()
{
int arr[10];
Classy c(arr);
c.printarray();
return 0;
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | d3coy |
| Solution 2 | |
| Solution 3 | Cave Dweller |
| Solution 4 | bulut |
