Dynamic Arrays

Can someone help me create a dynamic array for my default constructor? This is what I have so far and it doesn't feel right at all.
http://textuploader.com/dtsem

Here is the corresponding .h file
http://textuploader.com/dtse2

Thank you :)
In which class? What I'd recommend is just to use a std::vector, because it's basically a dynamic array. If you want a C-style dynamic array just do it like this

1
2
3
4
5
6
7
8
class Object
{
     int * myArray;
    
     public:
       Object(const unsigned & size): myArray(new int[size]){};

};
Last edited on
Unfortunately my professor only wants us to use a dynamic array otherwise i would've used a vector haha. But for GameofLife class!
You can mix c and c++ to make a monstrosity like this to put into your object:
(I didn't check this, coded on the fly here, but the syntax should be close). You could add a default ctor for 0 size / null pointer, but that seems pointless.

If you don't actually need to resize it, you can just do the above where size = maxsizeyoulleverneed. This is highly recommended, as the below is really fairly uncool (it gets the job done, but its like returning to 1990 or something).

struct dyna
{
unsigned int size;
double * d;
dyna(unsigned int s);
void resize(unsigned int s);
~dyna();
};

dyna::dyna(unsigned int s)
{
size = s;
d =malloc(s*sizeof(double));
}

dyna::~dyna()
{
size = 0;
free(d);
}

void dyna::resize(unsigned int s);
{
size = s;
d = realloc(d, s*sizeof(double));
}

Topic archived. No new replies allowed.