Initialize array in constructor

Question :

Hi, I've been programming in Java for 2 years and C/C++ for almost a year now. I'm still confused at some things because the difference of the languages.
I'd like to know how to initialize an array that's set as a data member of a class in the constructor.

Here's the code in Java :
public class ExpandableArray {
int Array[];
public final int m = 2;

public ExpandableArray(int number){
Array = new int[m*number];
}

}

how do I translate it to C++? I don't really know how you'd initialize the array in C++, because with Java, I use the "new" command.
Thanks for any help you can give me :)
1
2
3
4
5
6
7
8
9
10
11
class ExpandableArray {
private:
    int* Array; //Note the use of pointer here
    /*Other private fields here*/
public:
    int const /*<- instead of final*/ m = 2; //I do not know why do you need it, so I will leave it
    ExpandableArray(int number) //I will combine declaration with definition here for simlicity
    {
        Array = new int[m*number];
    }
}
And if you create the array using new don't forget to delete it when you no longer need it.
delete[] Array;
Yes, I forgot to mention it: you should delete it in destructor of your class:
1
2
3
4
~ExpandableArray()
{
    delete[] Array;
}
Thank you very much :)
Topic archived. No new replies allowed.