c++ - allocate a new memory for matrix -
i have 2 classes:
class class b: class
i want create matrix each pointer point b (mat of 1x2).
so defined:
a **mat; mat = new a*[1]; *mat = new b[2]; // call constructor.
now have 2 elements: mat[0][0], mat[0][1].
mat[0][0] initialized mat[0][1] null.
please help.
if sizeof(a) < sizeof(b)
array of a
's not equivalent array of b
's. since in second allocation allocate concreete objects larger *mat
supposed point to, when access second element doesn't access second b
element, rather slices first.
that's guess anyway, since didn't tell a
, b
.
edit
to answer comment, i'd start limiting amount of raw pointers use. use vector of vectors instead. each element of vector can raw pointer a
. can have run-time type of b
or c
.
#include <vector> std::vector<std::vector<a*> > mat(1, std::vector<a*>(2, (a*)0));
Comments
Post a Comment