-6

I wrote this class:

class Spacewalk {
private: 
    char mission[50];
    char date[50];
    char astronaut[50];
    char startingAt[50];
    char endingAt[50];
public:
    void list() {
       // lists the values.
    }
    void addValue(miss, da, astro, start, end) {
         // adds value to the private items.
    }
};

I created an array of this class, like this -

Spacewalk list[1]; 

Lets say, I've used up the space of this array, how would I increase this size of it?

Daksh M.
  • 4,589
  • 4
  • 30
  • 46
  • 4
    Use `std::vector x;` instead of `T x[N];`, then you can `.resize` or `.push_back`. – Dietrich Epp Aug 01 '17 at 14:46
  • 3
    You could use a `std::vector`. And perhaps `std::string` instead of char arrays? – Bo Persson Aug 01 '17 at 14:46
  • Did you referring to that? https://stackoverflow.com/questions/12032222/how-to-dynamically-increase-the-array-size – pakkk Aug 01 '17 at 14:47
  • the problem is std isn't available in turbo c++ and our goddamn school forces us to use it. – Daksh M. Aug 01 '17 at 14:47
  • Then that link with horrible manual unidiomatic ways of doing this might be just what you need :D / :S Bottom line: arrays are fixed-size, and don't make them small enough that you know you're going to saturate their size. – underscore_d Aug 01 '17 at 14:47
  • as others have mentioned, using std::vector is much efficient and most recommended. But as a side note, you could also use pointers to do your work but is awfully dirty. – knoxgon Aug 01 '17 at 14:48
  • Possible duplicate of [How to expand an array dynamically in C++? {like in vector }](https://stackoverflow.com/questions/1350630/how-to-expand-an-array-dynamically-in-c-like-in-vector) – underscore_d Aug 01 '17 at 14:49
  • 1
    @DakshMiglani The standard library is an integral part of c++. Learning c++ without access to the standard library is like learning to read and write without vowels. – François Andrieux Aug 01 '17 at 14:51
  • Guys, how many of you have used turbo c++? It does not have STL support. – Tanveer Badar Aug 01 '17 at 14:52
  • @TanveerBadar That was not originally tagged or mentioned, so the comments assuming a good compiler were perfectly fair. – underscore_d Aug 01 '17 at 14:54
  • Simple solution: get your school-money back. It's like a driving school teaching how to use a horse-carriage instead of a car. No reasonable developer uses TC for new projects. A school should teach current (i.e. since ca. 19 years) techniques. Install a modern toolchain, there are free ones available. – too honest for this site Aug 01 '17 at 14:55

1 Answers1

1

Arrays are great for learning the concepts of coding and as such I endorse them much more than any other standard template library (when it comes to learning code).

Note:
It is wise to use a vector however the reason schools don't teach this is because they want you to understand the underlying concepts behind things such as a vector, stack, or queue. You can't create a car without understanding the parts of it.

Sadly when it comes to resizing arrays there is no easy way other than to create a new array and transfer the elements. The best way to do this is to keep the array dynamic.

Note my example is for int(s) so you will have to make it into a template or change it to your desired class.

#include <iostream>
#include <stdio.h>
using namespace std;


static const int INCREASE_BY = 10;

void resize(int * pArray, int & size);


int main() {
    // your code goes here
    int * pArray = new int[10];
    pArray[1] = 1;
    pArray[2] = 2;
    int size = 10;
    resize(pArray, size);
    pArray[10] = 23;
    pArray[11] = 101;

    for (int i = 0; i < size; i++)
        cout << pArray[i] << endl;
    return 0;
}


void resize(int * pArray, int & size)
{
    size += INCREASE_BY;
    int * temp = (int *) realloc(pArray, size);
    delete [] pArray;
    pArray = temp;

}