Arrays

Hello, I'm a bit new to C++, I have great programming knowledge, currently i program in Java. However i want to get back into programming with C++ however my mind is very much into Java. I do understand some aspects of C++ but in Java programming, when your dealing with arrays, I've noticed when declaring an array in Java, you are able to predefine the array size with out it being a constant. Such as

int i = 10;

int[] numbers = new int[i];

This is how you could create an array of ints in Java with the size determined by i. But in C++ i would have to be a constant.

What I'm actually trying to do is create an object that will deal with adding elements to an array, the array will re-size its self, something simular to the vector class in C++ and also in Java it would be like the ArrayList. I cant seem to perform this line of code in C++ either

Object b_objs[10];

Object objs = b_objs;

In Java this is legal, but in C++ its not. I know this is very broad but there has to be some way i can resize an array, assign an array to a new array and so on.
For purely educational purposes, you can do the following in C++, with i being a variable:
int* numbers = new int[i];
However, that is rarely used in C++ as it would require manual deletion. What you're looking for is std::vector:
http://www.cplusplus.com/reference/stl/vector/

Since you already seem to know about std::vector, I'm confused as to what you actually want to do.
Arrays always have a constant size - if you need an variable size "array", you use std::vector (or a similar container).
Note that unlike C arrays, you can easily assign/copy/compare C++ arrays (std::array, std::tr1::array, boost::array).
Last edited on
Hi,

Create Array at C++


int *ptr = new int[10];
char *cptr = new char[10];
double *dptr = new double[10];


a single object:

int *ptr = new int;
char *cptr = new char;
double *dptr = new double;
Topic archived. No new replies allowed.