Is there anyway I can make a dynamic array of class objects??
eg in Java you can write
myclass [][] mc = new mc [5][6];
However in C++ I can't do something like that.
Do I have to use malloc??
Thanks in advance
malloc is C, not C++.
// Not-Dynamic
myclass mc[5][6];
or // Dynamic
1 2 3
|
myclass **mc = new mc*[5];
for (int i = 0; i < 5; ++i)
mc[i] = new mc[6];
|
or use a vector of vectors.
Last edited on
Zaita, that's the OO dev in you coming out again :).
malloc can sometimes be the only way of doing what you want.
I agree with your solution in this case but don't dismiss the old trusted malloc/free/calloc/realloc out of hand.
They all have their place in the C++ code toolbox.
Er..
Shouldn't that be
1 2 3
|
myclass **mc = new myclass*[5]; //Type is myclass, mc is instance
for (int i = 0; i < 5; ++i)
mc[i] = new myclass[6];
|
Last edited on
Good spot, I obviously wasn't concentrating on what he actually wrote :/