How cam i initialize a variable size array in a class in c++? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

How cam i initialize a variable size array in a class in c++?

Hello! I have the following problem. I would like to implement a variable size array in a class. Unfortunately, it does not work at all. Could anybody give me some advice? ... Or better a code as a blueprint. Thank you

5th Mar 2019, 11:13 PM
Nulpe
5 Answers
+ 5
If you need a "variable-sized array", you may want to use a vector instead? Just in case you weren't looking for dynamic arrays but instead arrays which "grow" on the run. https://en.cppreference.com/w/cpp/container/vector
6th Mar 2019, 12:46 AM
Fermi
Fermi - avatar
+ 4
First of all, you are missing the type for input_1. Since you are allocating an array of doubles the type should be double*. While assigning a variable on the same line is allowed since C++11, I think its better to move that to the constructor in this case. https://www.learncpp.com/cpp-tutorial/8-5a-constructor-member-initializer-lists/ Lastly you are trying to access a private member variable of the base class which is not allowed. Private members can only be accessed by its own class and by friends even with inheritance. This is where the protected access specifier is for. It means that the data under it can only be accessed by itself, friends and derived classes. Another way of going about it is to leave sizeInput1 under private and use getters instead. I recommend that you use a vector suggested by Fermi instead unless you want to learn how a vector works by implementing it yourself, so that you don't have to deal with new/delete and reallocations and the likes. Also you don't need ';''s after a function.
6th Mar 2019, 10:06 AM
Dennis
Dennis - avatar
+ 3
Since arr[n] where n is not a constant is illegal c++ your only option is to use pointers and heap allocation using new[] and delete[]. If all you need is an array that doesn't resize after allocation you don't need to worry about reallocation. The idea is to keep 3 variables in your class. 1. You need a variable that points to the heap, this variable will serve as the container holding all the elements and is allocated using new[] and deallocated using delete[]. 2. Since a pointer pointing to the heap does not know what it's size is you need another variable that keeps track of the size of the array, also known as the 'capacity'. 3. You need another variable that keeps track of how many elements are inside the array, this variable is called the 'size'. When you add an element to the array via a method, for example push_back that vector uses, you first do a check to see if size is equal to the capacity. If they are equal that means your array is full and you need to reallocate the array. You simply allocate a new array with a bigger capacity and copy over the old contents, then deallocate the old array. After reallocation or when the size did not match the capacity you simply copy the data to the array at the index of size and increment size by 1. I just happen to have a dynamic array on sololearn so feel free to use it as reference: https://code.sololearn.com/c1xPtLmS9eTr/?ref=app
5th Mar 2019, 11:58 PM
Dennis
Dennis - avatar
+ 1
Thx Dennis Fermi ... Through your help, i found an acceptable solution at first ... I will continiue to study your comments https://code.sololearn.com/c2hnw7MR5avZ/?ref=app
6th Mar 2019, 3:04 PM
Nulpe
0
Hey Dennis ... I tried, but it does not work. Did you know, my error and how i can fix it? https://code.sololearn.com/c9hfzwDgS3z3/?ref=app https://code.sololearn.com/c9hfzwDgS3z3/?ref=app
6th Mar 2019, 2:38 AM
Nulpe